nest icon indicating copy to clipboard operation
nest copied to clipboard

How to setup useWebSocketAdapter in NestFactory.createApplicationContext

Open Insidexa opened this issue 7 years ago • 10 comments

I'm submitting a...


[ ] Regression 
[x ] Bug report
[x ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I create CLIModule and use nestjs-command package. In cli mode I run queue and use websocket for notifications on end job ( only push ). But NestFactory.createApplicationContext doesn't allow to setup redis-io adapter for websocket.

Expected behavior

Maybe add dynamic setup redis-io adapter if it possible. I understand, websocket needs express server. To solve problem, I use NestFactory.create but it slowly => running RouterExplorer.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.x

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Insidexa avatar May 14 '19 13:05 Insidexa

Hey @Insidexa If I understand what you want to do is to run a Nest application context to send commands through websocket using redis-io ?

In this case you can create a client in a service with the ClientProxyFactory if you want to send messages to microservices or a socket io client in your case to send them according to your program inputs. Nest Application Context is here to run programs like a Shell program but well structured and easier to maintain as test.

For example I developed a Nest Application Context that start a CronJob that send messages to a redis and a Nest Microservice Application that listen these messages to perform logic to build a newsletter and send it by emails.

Maybe in your case the problem is Nest does not provide a way to create WebSocket Client out of the box ? Is that answer your question ?

thomrick avatar May 21 '19 10:05 thomrick

@ThomRick you suggest me to create nest microservice to listen events from my queue by some protocol. In microservice on every event -> push to websocket ? Yes ?

microservice app type allow to using useWebSocketAdapter

or connect to redis, and publish with ClientProxyFactory and publish message to socket-io redis adapter ?

Insidexa avatar May 21 '19 11:05 Insidexa

You consume your queue messages over WebSocket right ? I probably choose a Microservice with WebSockerAdapter to consume the queue but I never tested that.

I can build a POC to see how it could be managed. :-)

thomrick avatar May 21 '19 12:05 thomrick

@ThomRick no, I use bulljs. And after job completed in queue I need to notify client ( user ) by websockets.

Insidexa avatar May 21 '19 12:05 Insidexa

Ok so you need to notify your websocket server (Web Application or Microservice) to send client notifications after bulljs job completed (and your first try was to use Application Context).

So yeah you can use Application Context Application to handle your job queue consumption and use a websocket client with a Custom Provider to notify your websocket server that will broadcast events to your users.

Or share the the WebSocketServer instance to your queue job handler to notify right after completed

thomrick avatar May 21 '19 13:05 thomrick

@ThomRick problem solved via https://github.com/socketio/socket.io-emitter But need operate namespaces when emitting to websocket io.of('/example').to('example-0').emit('test', payload); Maybe include socket.io-emitter to nestjs docs ( link ) or implement websocket decorator Emitter()

@WebSocketGateway({ path: '/ws', namespace: '/example', transports: ['websocket'] })
export class WSGateway {
    @WebSocketServer()
    private server: Server;

    @Emitter()
    private emitter: Emitter; // use if server undefined in non-socket.io processes
}

Insidexa avatar May 21 '19 14:05 Insidexa

Nice ! 👍

But in a @nestjs/platform-socket.io WebSocketServer is defined so I think it is useless to define a WebSocketGateway in a non-socket.io process and integrate an Emitter decorator.

But it could be awesome to have a WebSocketClient like Microservices ClientProxy that using the socket.io-emitter as you did ;-)

thomrick avatar May 21 '19 15:05 thomrick

You are right ) Maybe proof of concept

@WebSocketClient({ of: WebSocketGateway })
class WSClient {
 @Emitter() private emitter: WebSocketEmitter; // emitter get config from websocket server ( path, namespace, etc.... )

 public updateCount(count) {
    this.emitter.emit('counter', count)
  }
}

Insidexa avatar May 21 '19 17:05 Insidexa

Maybe something more like this :

@WebSocketClient({ of: WebSocketGateway })
class WSCounter {
  public updateCount(count): WebSocketClientResponse<any> {
    return { event: 'counter', data: count };
  }
}

The emitter is created through the @WebSocketClient() decorator ;-)

thomrick avatar May 22 '19 11:05 thomrick

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 30 '19 13:09 lock[bot]