0

Can someone please help and tell me how I can write the following code snippet which is written as a node application to write as a nest application with just WebSockets and gateway class?

    var webSocketsServerPort = 8080;
    var webSocketServer = require('websocket').server;
    var http = require('http');
    var server = http.createServer(function(request, response) {
    });
    server.listen(webSocketsServerPort, function() {
    console.log(
    new Date() + ' Server is listening on port ' + webSocketsServerPort
        );
    );
    });
    var wsServer = new webSocketServer({
    httpServer: server
    });
    wsServer.on('request', function(request) {
    var connection = request.accept(null, request.origin);
    connection.on('message', function(data) {
    var message = JSON.parse(data.utf8Data);
    });

    connection.on('close', function(connection) {});

EDIT: In main.ts I wrote this : app.useWebSocketAdapter(new WsAdapter(app));

This is how my gateway class looks like:

import { WebSocketGateway, WebSocketServer, OnGatewayInit, OnGatewayDisconnect, OnGatewayConnection, SubscribeMessage } from "@nestjs/websockets";
import { Server } from "websocket";
import { Logger } from "@nestjs/common";

@WebSocketGateway(8080)
export class ChatterGateway implements OnGatewayInit, OnGatewayDisconnect, OnGatewayConnection {
    @WebSocketServer() private server: server;

    private logger: Logger = new Logger('ChatterGateway');

    afterInit(server: server) {
        this.logger.log('ChatGetway init');
    }

    handleConnection(client: any, data) {
        this.logger.log('Client connected');
    }

    handleDisconnect(client: any) {
        this.logger.log('Client disconnected');
    }

    @SubscribeMessage('request')
    handleRequest(client: any, data) {
        this.logger.log(data);
    }

    @SubscribeMessage('message')
    handleMessage(client: any, data) {
        this.logger.log(data);
    }
}
Hans123
  • 33
  • 1
  • 5
  • [Have you looked through the documentation](https://docs.nestjs.com/websockets/gateways)? Also, your websocket server doesn't do anything, it just accepts the connection and locally stores a variable that it does nothing with. – Jay McDoniel Mar 31 '20 at 17:22
  • Yeah, I looked through the documentation already and also tried it that way. The problem is that when I try to implement that with nestjs gateways, then I always have to implement the @SubscribeMessage() decorator. But that way the listener doesn't listen to any default events like request,message or close. The websocket library im trying with is the pure javascript one WebSocket for node.js – Hans123 Mar 31 '20 at 17:50
  • And yes, the websocket server doesn't do anything cuz I just wanted to know how it'll look like if it's coded in a nestjs application. – Hans123 Mar 31 '20 at 17:56
  • You can absolutely set a `@SubscribeMessage()` to listen to `message`. Have you made sure to set the proper websocket adapter in your `main.ts`? Showing what you've tried, rather than asking for an example usually gets better results in debugging and finding how to set things up. – Jay McDoniel Mar 31 '20 at 18:17
  • I now added what I already have. I used the WsAdapter from ws. Is that correct? – Hans123 Mar 31 '20 at 19:16
  • That all looks correct. Now you _should_ just need to start the server and connect via `new Websocket('ws://localhost:8080');` – Jay McDoniel Mar 31 '20 at 19:27
  • new Websocket('ws://localhost:8080'); is for the client side right? On the client side I have a flutter app with the websocket library web_socket_channel. Is it able to connect to a flutter app with web_socket_channel? I tried almost everything but nothing worked. But the node application from above worked and is able to connect, receive and send data to my flutter app. The thing is that I wanted it in a gateway class/nest.js code but not able to do that. When I implement that node application from above in the bootstrap function in main.ts then it also works. – Hans123 Mar 31 '20 at 19:39
  • How are you binding the gateway to the server? It's added in a providers array of a module and that module, if it is not the root module, as added to the root module and eventually supplied to the NestFactory, correct? I haven't worked with Flutter, but yes, the websocket connection I wrote should be from the client side. If you're still having issues, maybe you can provide your repository code that publicly available. – Jay McDoniel Mar 31 '20 at 19:42
  • Yeah, it's added in the providers array and everything works fine. It even logs data, when the client connects(the handleConnection function gets executed). But whenever I try to send an event from the client side with the same event name as in the SubscribeMessage decorator and some data, the other functions handleRequest and handleMessage never get executed. – Hans123 Mar 31 '20 at 20:34
  • Again, I've never used Flutter so it may be an issue of how Flutter's `web_socket_channel` is trying to send the event. I'd look into their docs and see if it works, and also try from a Node REPL and see if connecting with a raw WebSocket class works as expected. If not, then you've got an issue, but I know I've had success from the Node REPL and WebSocket in the past – Jay McDoniel Mar 31 '20 at 21:05
  • Ok. Anyways, thank you very much :) – Hans123 Mar 31 '20 at 21:09

0 Answers0