(no title)
yasserf | 1 year ago
const onConnect: ChannelConnection<'hello!'> = async (services, channel) => {
// On connection (like onOpen)
channel.send('hello') // This is checked against the input type
}
const onDisconnect: ChannelDisconnection = async (services, channel) => {
// On close
// This can't send anything since channel closed
}
const onMessage: ChannelMessage<'hello!' | { name: string }, 'hey'> = async (services, channel) => {
channel.send('hey')
}
export const subscribeToLikes: ChannelMessage<
{ talkId: string; action: 'subscribeToLikes' },
{ action: string; likes: number }
> = async (services, channel, { action, talkId }) => {
const channelName = services.talks.getChannelName(talkId)
// This is a service that implements a pubsub/eventhub interface
await services.eventHub.subscribe(channelName, channel.channelId)
// we return the action since the frontend can use it to route to specific listeners as well (this could be absorbed by vrameworks runtime in future)
return { action, likes: await services.talks.getLikes(talkId) }
}
addChannel({
name: 'talks',
route: '/',
auth: true,
onConnect,
onDisconnect,
// Default message handler
onMessage,
// This will route the message to the correct function if a property action exists with the value subscribeToLikes (or otherwise)
onMessageRoute: {
action: {
subscribeToLikes: {
func: subscribeToLikes,
permissions: {
isTalkMember: [isTalkMember, isNotPresenter],
isAdmin
},
},
},
},
})
A code example.Worth noting you can share functions across websockets as well, which allows you to compose logic across different ones if needed
No comments yet.