Skip to content

Commit ebb7a48

Browse files
committed
adding rpc demo
1 parent 179be77 commit ebb7a48

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

demos/rpc/client.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {ConnectionInfo, ResponseMessage} from '../../libs/interfaces'
2+
import {RealtimeClient} from '../../libs'
3+
import {getAuthToken, config} from './../config'
4+
5+
const client = new RealtimeClient({
6+
websocketOptions: {
7+
maxRetries: 10,
8+
urlProvider: async () => {
9+
const ACCESS_TOKEN = getAuthToken(
10+
config.WEBSOCKET_CLIENTS_SIGNING_KEY,
11+
5,
12+
{
13+
permissions: [],
14+
},
15+
config.ALGORITHM,
16+
)
17+
18+
return `wss://${config.CLUSTER_HOSTNAME}/apps/${config.APP_ID}?access_token=${ACCESS_TOKEN}`
19+
},
20+
},
21+
})
22+
23+
client.on('session.started', async (info: ConnectionInfo) => {
24+
// request time to server
25+
const [res] = await client
26+
.send('', {
27+
messageType: 'gettime',
28+
})
29+
.waitForReply()
30+
31+
console.log('Server Time:', res as ResponseMessage)
32+
33+
client.disconnect()
34+
})
35+
36+
client.connect()

demos/rpc/server.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {ConnectionInfo, ReplyFunction} from './../../libs/interfaces'
2+
import {RealtimeClient} from './../../libs'
3+
import {getAuthToken, config} from './../config'
4+
5+
const client = new RealtimeClient({
6+
websocketOptions: {
7+
maxRetries: 10,
8+
urlProvider: async () => {
9+
const ACCESS_TOKEN = getAuthToken(
10+
config.WEBSOCKET_CLIENTS_SIGNING_KEY,
11+
5,
12+
{
13+
permissions: [
14+
'realtime:publisher:write:topic:priv/*',
15+
'realtime:subscriber:read:topic:secure/inbound',
16+
],
17+
},
18+
config.ALGORITHM,
19+
)
20+
21+
return `wss://${config.CLUSTER_HOSTNAME}/apps/${config.APP_ID}?access_token=${ACCESS_TOKEN}`
22+
},
23+
},
24+
})
25+
26+
client.on('session.started', async (info: ConnectionInfo) => {
27+
client.subscribeRemoteTopic('secure/inbound')
28+
})
29+
30+
client.on('secure/inbound.gettime', async (_, reply: ReplyFunction) => {
31+
console.log('Responding to gettime request...')
32+
33+
await reply(new Date().toISOString(), 'ok').waitForAck()
34+
})
35+
36+
client.connect()

0 commit comments

Comments
 (0)