Skip to content

Once I've set up a socket.io connection between server.js and client.js, is it possible to also connect to an external websocket API, and broadcast it to the internal connection? #4490

Discussion options

You must be logged in to vote

Hi! You should be able to pipe the events from the source to the Socket.IO connection:

import { Server } from "socket.io";
import WebSocket from "ws";

const io = new Server(3000);

const client = new WebSocket("wss://stream.example.com:5555/ws/GMT4@weatherdata");

client.on("message", (data) => {
  io.emit("weatherdata", data);
});

// TODO handle reconnection to the source

Does it suit your use case?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@respectabiggle
Comment options

Answer selected by respectabiggle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants