Adapters

The WebSockets module is platform-agnostic, hence, you can bring your own library (or even a native implementation) by making use of WebSocketAdapter interface. This interface forces to implement few methods described in the following table:

createCreates a socket instance based on passed arguments
bindClientConnectBinds the client connection event
bindClientDisconnectBinds the client disconnection event (optional*)
bindMessageHandlersBinds the incoming message to the corresponding message handler
closeTerminates a server instance

Extend socket.io

The socket.io package is wrapped in an IoAdapter class. What if you would like to enhance the basic functionality of the adapter? For instance, your technical requirements require a capability to broadcast events across multiple load-balanced instances of your web service. For this, you can extend IoAdapter and override a single method which responsibility is to instantiate new socket.io servers. But first of all, let’s install the required package.

  1. $ npm i --save socket.io-redis

Once the package is installed, we can create a RedisIoAdapter class.

  1. import { IoAdapter } from '@nestjs/platform-socket.io';
  2. import * as redisIoAdapter from 'socket.io-redis';
  3. export class RedisIoAdapter extends IoAdapter {
  4. createIOServer(port: number, options?: any): any {
  5. const server = super.createIOServer(port, options);
  6. const redisAdapter = redisIoAdapter({ host: 'localhost', port: 6379 });
  7. server.adapter(redisAdapter);
  8. return server;
  9. }
  10. }

Afterward, simply switch to your newly created Redis adapter.

  1. const app = await NestFactory.create(ApplicationModule);
  2. app.useWebSocketAdapter(new RedisIoAdapter(app));

Ws library

Another available adapter is a WsAdapter which in turn acts like a proxy between the framework and integrate blazing fast and thoroughly tested ws library. This adapter is fully compatible with native browser WebSockets and is far faster than socket.io package. Unluckily, it has significantly fewer functionalities available out-of-the-box. In some cases, you may just don’t necessarily need them though.

In order to use ws, we firstly have to install the required package:

  1. $ npm i --save @nestjs/platform-ws

Once the package is installed, we can switch an adapter:

  1. const app = await NestFactory.create(ApplicationModule);
  2. app.useWebSocketAdapter(new WsAdapter(app));

Hint The WsAdapter is imported from @nestjs/platform-ws.

Advanced (custom adapter)

For demonstration purposes, we are going to integrate the ws library manually. As mentioned, the adapter for this library is already created and is exposed from the @nestjs/platform-ws package as a WsAdapter class. Here is how the simplified implementation could potentially look like:

ws-adapter.ts

  1. import * as WebSocket from 'ws';
  2. import { WebSocketAdapter, INestApplicationContext } from '@nestjs/common';
  3. import { MessageMappingProperties } from '@nestjs/websockets';
  4. import { Observable, fromEvent, EMPTY } from 'rxjs';
  5. import { mergeMap, filter } from 'rxjs/operators';
  6. export class WsAdapter implements WebSocketAdapter {
  7. constructor(private app: INestApplicationContext) {}
  8. create(port: number, options: any = {}): any {
  9. return new ws.Server({ port, ...options });
  10. }
  11. bindClientConnect(server, callback: Function) {
  12. server.on('connection', callback);
  13. }
  14. bindMessageHandlers(
  15. client: WebSocket,
  16. handlers: MessageMappingProperties[],
  17. process: (data: any) => Observable<any>,
  18. ) {
  19. fromEvent(client, 'message')
  20. .pipe(
  21. mergeMap(data => this.bindMessageHandler(data, handlers, process)),
  22. filter(result => result),
  23. )
  24. .subscribe(response => client.send(JSON.stringify(response)));
  25. }
  26. bindMessageHandler(
  27. buffer,
  28. handlers: MessageMappingProperties[],
  29. process: (data: any) => Observable<any>,
  30. ): Observable<any> {
  31. const message = JSON.parse(buffer.data);
  32. const messageHandler = handlers.find(
  33. handler => handler.message === message.event,
  34. );
  35. if (!messageHandler) {
  36. return EMPTY;
  37. }
  38. return process(messageHandler.callback(message.data));
  39. }
  40. close(server) {
  41. server.close();
  42. }
  43. }

Hint When you want to take advantage of ws library, use built-in WsAdapter instead of creating your own one.

Then, we can set up a custom adapter using useWebSocketAdapter() method:

main.ts

  1. const app = await NestFactory.create(ApplicationModule);
  2. app.useWebSocketAdapter(new WsAdapter(app));

Example

A working example that uses WsAdapter is available here.