user6680
user6680

Reputation: 139

Two different websockets working together

I'm trying to figure out how to implement a two separate websockets together. Not sure if this possible or not, but I have a websocket that works on it's own in from node.js file to angular another node.js file that uses Kraken (crypto exchange) websocket that also works in it's own file. I'm trying to consolidate them both together so that whenever a event onChange comes from Kraken websocket, I can relay that data to angular with angular socket.io websocket. Trying to do something like this

  const webSocketClient = new WebSocket(connectionURL);

      webSocketClient.on("open", function open() {
        webSocketClient.send(webSocketSubscription);
      });

      webSocketClient.on("message", function incoming(wsMsg) {
        const data = JSON.parse(wsMsg);

       let io = require("socket.io")(server, {
          cors: {
            origin: "http://localhost:4200",
            methods: ["GET", "POST"],
            allowedHeaders: ["*"],
            credentials: true,
          },
        });


         io.on("connection", (socket) => {
           const changes = parseTrades(data);
              socketIo.sockets.emit(connection.change, changes);
            Log whenever a user connects
          console.log("user connected");

           socket.emit("test event", JSON.stringify(changes));
         });
        console.log("DATA HERE", data[0]);
      });

      webSocketClient.on("close", function close() {
        console.log("kraken websocket closed");
      });

Although doing this doesnt relay the data to frontend and gives me a memory leak. Is there some way I can accomplish this?

Upvotes: 0

Views: 168

Answers (1)

I would probably split up the task a little bit. So have a service for the kraken websocket and maybe a service for your own socket, then have them communicate via observables, that you can also tap into from the front end to display data you want.

@Injectable()
export class KrakenService{
 private webSocketClient : WebSocket | null;
 private messages$ = new BehaviorSubject<any>(null); // give it a type 
 openConnection(){
  // is connectionUrl from environment ??
  this.webSocketClient = new WebSocket(connectionURL);

      webSocketClient.on("open", function open() {
        /// is webSocketSubscription from environment ??
        webSocketClient.send(webSocketSubscription);
      });

      webSocketClient.on("message", function incoming(wsMsg) {
        const data = JSON.parse(wsMsg);
        this.messages$.next(data);
        console.log("DATA HERE", data[0]);
      });

      webSocketClient.on("close", function close() {
        console.log("kraken websocket closed");
        this.webSocketClient = null;
      });
  }
  getKrakenMessages(){
   if(webSocketClient == null) this.openConnection();
   return messages$.asObserbable();
  }
}

So now when you want to either read the web socket messages or use them with the other socket you just subscribe to the krakenService.getKrakenMessages();

Then you can do something similar with you local service as well. Have something that opens connections, and one that emits messages. The example you showed it would open up a connection every time you got a message, so keep those logics separated. And reuse existing connection.

Upvotes: 1

Related Questions