Created
September 3, 2020 19:00
-
-
Save bbangert/6572ddf95bd446d3ba560f8ee595d00b to your computer and use it in GitHub Desktop.
Customized message dispatch/handler for NestJS WS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { WsAdapter } from '@nestjs/platform-ws'; | |
import { MessageMappingProperties } from '@nestjs/websockets'; | |
import { EMPTY as empty, Observable } from 'rxjs'; | |
export class CustomWsAdapter extends WsAdapter { | |
public bindMessageHandler( | |
buffer: any, | |
handlers: MessageMappingProperties[], | |
transform: (data: any) => Observable<any>, | |
): Observable<any> { | |
try { | |
const message = JSON.parse(buffer.data); | |
const messageHandler = handlers.find( | |
(handler) => handler.message === 'message', | |
); | |
const { callback } = messageHandler; | |
return transform(callback(message)); | |
} catch { | |
return empty; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you are the best