wildflock/src/channels.ts

39 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-29 18:10:00 +00:00
// For more information about this file see https://dove.feathersjs.com/guides/cli/channels.html
2023-08-06 06:21:17 +00:00
import type { RealTimeConnection, Params } from '@feathersjs/feathers';
import type { AuthenticationResult } from '@feathersjs/authentication';
import '@feathersjs/transport-commons';
import type { Application, HookContext } from './declarations';
import { logger } from './logger';
2023-07-29 18:10:00 +00:00
export const channels = (app: Application) => {
2023-08-06 06:21:17 +00:00
logger.warn(
'Publishing all events to all authenticated users. See `channels.ts` and https://dove.feathersjs.com/api/channels.html for more information.'
);
2023-07-29 18:10:00 +00:00
2023-08-06 06:21:17 +00:00
app.on('connection', (connection: RealTimeConnection) => {
// On a new real-time connection, add it to the anonymous channel
app.channel('anonymous').join(connection);
});
2023-07-29 18:10:00 +00:00
2023-08-06 06:21:17 +00:00
app.on('login', (authResult: AuthenticationResult, { connection }: Params) => {
// connection can be undefined if there is no
// real-time connection, e.g. when logging in via REST
if (connection) {
// The connection is no longer anonymous, remove it
app.channel('anonymous').leave(connection);
2023-07-29 18:10:00 +00:00
2023-08-06 06:21:17 +00:00
// Add it to the authenticated user channel
app.channel('authenticated').join(connection);
}
});
2023-07-29 18:10:00 +00:00
2023-08-06 06:21:17 +00:00
// eslint-disable-next-line no-unused-vars
app.publish((data: any, context: 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, () => {})`
2023-07-29 18:10:00 +00:00
2023-08-06 06:21:17 +00:00
// e.g. to publish all service events to all authenticated users use
return app.channel('authenticated');
});
2023-07-29 21:50:42 +00:00
};