Open goodmanpersonguy opened 4 years ago
This exactly what I'm looking for as well. I want to build a Nest Websocket Client that connects to a Cryptocurrency exchange's websocket server for market/pricing data, and then push received messages into Kafka. While I could easily do this without Nest; the data feed service is just part of an overall automated trading system that has many other components built using Nest.
Until an official solution is provided, my plan is to build a custom transport strategy loosely based on this documentation: https://docs.nestjs.com/microservices/custom-transport
Here's a few more resources that I think will be helping in hacking together a websockets client; they at least resemble the use case:
Any other resources that are a little more direct, would be much appreciated!
Hey guys @goodmanpersonguy @cnaccio ! Any luck with figuring out how to properly make Nestjs WebSocket Client application/module? Definitely worth to update documentation on how to work with WS client in Nestjs.
Also looking for this. We have multiple servers. All are web-socket servers. But some are also clients of the other servers.
I'm also trying to solve this. Nest Websocket Client connecting to Nest Websocket Server. Any solutions so far?
I'm looking for that too. Too bad it still doesn't have socket client support.
I found this article series useful. Although it deals with NATS transport, the concept applies to other transports too.
@goodmanpersonguy @cnaccio ! I found this example for angular, since nestjs is based on angular I implemented the same solution that they raised for angular and it worked (sorry for my bad english)
nest g resource Socket
in socketService implement the methods for the socket
import {Injectable} from '@ nestjs / common';
import * as io from 'socket.io-client';
import {Observable} from 'rxjs';
import {env} from 'node: process';
@Injectable ()
export class SocketService {
private url = process.env.SOCKET_URL;
private socket;
connect () {
this.socket = io (this.url);
}
emit (emitName: string, data?) {
this.socket.emit (emitName, data);
}
on (onName: string) {
let observable = new Observable ((observer) => {
this.socket.on (onName, (data) => {
observer.next (data);
});
return () => {
this.socket.disconnect ();
};
});
return observable;
}
}
in socketGateway implement your use
import {
WebSocketGateway,
OnGatewayInit,
OnGatewayDisconnect
} from '@ nestjs / websockets';
import {SocketService} from './socket.service';
@WebSocketGateway ()
export class SocketGateway implements OnGatewayInit, OnGatewayDisconnect {
connection: any;
data: any;
message: any;
constructor (private readonly socketService: SocketService) {}
afterInit () {
this.socketService.connect ();
this.connection = this.socketService.on ('connect'). subscribe ((data) => {
console.log (data);
this.message = data;
});
let message = {
service: '' asdsadsad ",
ip: "",
port: ""
};
this.socketService.emit ('notify', message);
}
handleDisconnect (client: any) {
throw new Error ('Method not implemented.');
}
}
any working solutions?
@goodmanpersonguy I just released a module for use websocket-client with nestjs. I'd love you feedback guys :D
cc: @cnaccio @Perf @MulderNick @StefanErler @investeusa @zub0r @lrlango @lrlago
@goodmanpersonguy I just released a module for use websocket-client with nestjs. I'd love you feedback guys :D
cc: @cnaccio @Perf @MulderNick @StefanErler @investeusa @zub0r @lrlango @lrlago
thanks, i used your module. However, I have a problem as follows, when the connection to the server side is closed, now I don't know how to reopen that connection. I'd love you feedback guys :D
I followed this documentation and it worked perfectly. Hope it helps you guys in some way. They're sorry if I got my English wrong.
I followed this documentation and it worked perfectly. Hope it helps you guys in some way. They're sorry if I got my English wrong.
Happy to hear that, let me know if you have any problems
@goodmanpersonguy I just released a module for use websocket-client with nestjs. I'd love you feedback guys :D
cc: @cnaccio @Perf @MulderNick @StefanErler @investeusa @zub0r @lrlango @lrlago
thanks, i used your module. However, I have a problem as follows, when the connection to the server side is closed, now I don't know how to reopen that connection. I'd love you feedback guys :D
Sorry to hear that, can you push a repository with the implementation? Or open an issue in the repository of nestjs-websocket with the steps need it to reproduce the problem.
Thanks
I followed this documentation and it worked perfectly. Hope it helps you guys in some way. They're sorry if I got my English wrong. https://www.npmjs.com/package/nestjs-websocket
Happy to hear that, let me know if you have any problems
Below is my module
import { Module, HttpModule, Injectable } from '@nestjs/common';
import { TasksService } from './tasks.service';
import { WebSocketModule } from 'nestjs-websocket';
@Injectable()
class ConfigService {
public readonly url = 'wss://youendipoint.com./';
}
@Module({
imports: [
HttpModule,
WebSocketModule.forRootAsync({
providers: [ConfigService],
inject: [ConfigService],
useFactory: (config: ConfigService) => {
return {
url: config.url,
};
},
}),
],
providers: [TasksService],
})
export class TasksModule {}
Below is my service
import { Injectable, Logger } from '@nestjs/common';
import {
InjectWebSocketProvider,
WebSocketClient,
OnOpen,
OnMessage,
} from 'nestjs-websocket';
@Injectable()
export class TasksService {
private data: Record<any, any> = {};
constructor(
@InjectWebSocketProvider()
private readonly ws: WebSocketClient,
) {}
@OnOpen()
open() {
console.log('The connection is established.');
this.ws.send(
JSON.stringify({payload: "payload"}),
);
}
@OnMessage()
message(data: WebSocketClient.Data) {
this.data = JSON.parse(data.toString());
console.log(this.data);
}
}
Very simple ;)
I'm submitting a...
Current behavior
So the docs here
Gateways
describe really well, how to create a websocket server. Unfortunately I didn't find anything about a client setup.
Expected behavior
I would expect an additional chapter for socket clients
What is the motivation / use case for changing the behavior?
Currently I have a websocket server with a standard frontend client. I want to extend this project with a second Nest backend which should also connect as a socket client. It would be nice to have a tutorial on how to make this Nest application a socket client listening to events etc.