Skip to content

Commit

Permalink
adding rpc demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Sep 19, 2024
1 parent 179be77 commit ebb7a48
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
36 changes: 36 additions & 0 deletions demos/rpc/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {ConnectionInfo, ResponseMessage} from '../../libs/interfaces'
import {RealtimeClient} from '../../libs'
import {getAuthToken, config} from './../config'

const client = new RealtimeClient({
websocketOptions: {
maxRetries: 10,
urlProvider: async () => {
const ACCESS_TOKEN = getAuthToken(
config.WEBSOCKET_CLIENTS_SIGNING_KEY,
5,
{
permissions: [],
},
config.ALGORITHM,
)

return `wss://${config.CLUSTER_HOSTNAME}/apps/${config.APP_ID}?access_token=${ACCESS_TOKEN}`
},
},
})

client.on('session.started', async (info: ConnectionInfo) => {
// request time to server
const [res] = await client
.send('', {
messageType: 'gettime',
})
.waitForReply()

console.log('Server Time:', res as ResponseMessage)

client.disconnect()
})

client.connect()
36 changes: 36 additions & 0 deletions demos/rpc/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {ConnectionInfo, ReplyFunction} from './../../libs/interfaces'
import {RealtimeClient} from './../../libs'
import {getAuthToken, config} from './../config'

const client = new RealtimeClient({
websocketOptions: {
maxRetries: 10,
urlProvider: async () => {
const ACCESS_TOKEN = getAuthToken(
config.WEBSOCKET_CLIENTS_SIGNING_KEY,
5,
{
permissions: [
'realtime:publisher:write:topic:priv/*',
'realtime:subscriber:read:topic:secure/inbound',
],
},
config.ALGORITHM,
)

return `wss://${config.CLUSTER_HOSTNAME}/apps/${config.APP_ID}?access_token=${ACCESS_TOKEN}`
},
},
})

client.on('session.started', async (info: ConnectionInfo) => {
client.subscribeRemoteTopic('secure/inbound')
})

client.on('secure/inbound.gettime', async (_, reply: ReplyFunction) => {
console.log('Responding to gettime request...')

await reply(new Date().toISOString(), 'ok').waitForAck()
})

client.connect()

0 comments on commit ebb7a48

Please sign in to comment.