import '@feathersjs/transport-commons' import { HookContext } from '@feathersjs/feathers' import { Application } from './declarations' export default (app: Application): void => { if (typeof app.channel !== 'function') { // If no real-time functionality has been configured just return return } app.on('connection', (connection: any) => { // On a new real-time connection, add it to the anonymous channel app.channel('anonymous').join(connection) }) app.on('login', (authResult: any, { connection }: any) => { // connection can be undefined if there is no // real-time connection, e.g. when logging in via REST if (connection) { // Obtain the logged in user from the connection // const user = connection.user // The connection is no longer anonymous, remove it app.channel('anonymous').leave(connection) // Add it to the authenticated user channel app.channel('authenticated').join(connection) // Channels can be named anything and joined on any condition // E.g. to send real-time events only to admins use // if(user.isAdmin) { app.channel('admins').join(connection) } // If the user has joined e.g. chat rooms // if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(`rooms/${room.id}`).join(channel)) // Easily organize user by email and userid for things like messaging // app.channel(`emails/${user.email}`).join(channel) // eslint-disable-next-line @typescript-eslint/restrict-template-expressions app.channel(`userIds/${connection['identity-provider'].userId}`).join(connection) } }) app.publish((data: any, hook: HookContext) => { // Here you can add event publishers to channels set up in `channels.js` // To publish only for a specific event use `app.publish(eventname, () => {})` // console.log(data) // console.log(hook) // console.log('Publishing all events to all authenticated user. See `channels.js` and https://docs.feathersjs.com/api/channels.html for more information.') // eslint-disable-line // e.g. to publish all service events to all authenticated user use return app.channel('authenticated') }) // Here you can also add service specific event publishers // e.g. the publish the `user` service `created` event to the `admins` channel // app.service('user').publish('created', () => app.channel('admins')) // With the userid and email group from above you can easily select involved user // app.service('messages').publish(() => { // return [ // app.channel(`userIds/${data.createdBy}`), // app.channel(`emails/${data.recipientEmail}`) // ] // }) app.service('chatroom').publish('created', data => { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions return app.channel(`userIds/${data.sender_id}`).send({ conversation: data.conversation }) }) }