diff --git a/packages/transport-commons/src/socket/index.ts b/packages/transport-commons/src/socket/index.ts index 83b0ee2af0..89c1f57105 100644 --- a/packages/transport-commons/src/socket/index.ts +++ b/packages/transport-commons/src/socket/index.ts @@ -11,10 +11,11 @@ export interface SocketOptions { done: Promise; emit: string; socketMap: WeakMap; + socketKey?: any; getParams: (socket: any) => RealTimeConnection; } -export function socket ({ done, emit, socketMap, getParams }: SocketOptions) { +export function socket ({ done, emit, socketMap, socketKey, getParams }: SocketOptions) { return (app: Application) => { const leaveChannels = (connection: RealTimeConnection) => { const { channels } = app; @@ -27,7 +28,7 @@ export function socket ({ done, emit, socketMap, getParams }: SocketOptions) { app.configure(channels()); app.configure(routing()); - app.on('publish', getDispatcher(emit, socketMap)); + app.on('publish', getDispatcher(emit, socketMap, socketKey)); app.on('disconnect', leaveChannels); app.on('logout', (_authResult: any, params: Params) => { const { connection } = params; diff --git a/packages/transport-commons/src/socket/utils.ts b/packages/transport-commons/src/socket/utils.ts index 73d6dc1995..eccf5f5cfa 100644 --- a/packages/transport-commons/src/socket/utils.ts +++ b/packages/transport-commons/src/socket/utils.ts @@ -35,13 +35,13 @@ export function normalizeError (e: any) { return result; } -export function getDispatcher (emit: string, socketMap: WeakMap) { - return function (event: string, channel: CombinedChannel, context: HookContext, data: any) { +export function getDispatcher (emit: string, socketMap: WeakMap, socketKey?: any) { + return function (event: string, channel: CombinedChannel, context: HookContext, data?: any) { debug(`Dispatching '${event}' to ${channel.length} connections`); channel.connections.forEach(connection => { // The reference between connection and socket is set in `app.setup` - const socket = socketMap.get(connection); + const socket = socketKey ? connection[socketKey] : socketMap.get(connection); if (socket) { const eventName = `${context.path || ''} ${event}`.trim(); diff --git a/packages/transport-commons/test/socket/utils.test.ts b/packages/transport-commons/test/socket/utils.test.ts index b7bba8faba..b2bc57d3aa 100644 --- a/packages/transport-commons/test/socket/utils.test.ts +++ b/packages/transport-commons/test/socket/utils.test.ts @@ -63,6 +63,28 @@ describe('socket commons utils', () => { assert.strictEqual(typeof getDispatcher('test', new WeakMap()), 'function') ); + it('works with backwards compatible socketKey', done => { + const socketKey = Symbol('@feathersjs/test'); + const dispatcher = getDispatcher('emit', undefined, socketKey); + const socket = new EventEmitter(); + const connection = { + [socketKey]: socket + }; + const channel: any = { + connections: [ connection ], + dataFor (): null { + return null; + } + }; + + socket.once('testing', data => { + assert.strictEqual(data, 'hi'); + done(); + }); + + dispatcher('testing', channel, { result: 'hi' } as any); + }); + describe('dispatcher logic', () => { let dispatcher: any; let dummySocket: EventEmitter;