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
Answered
by
darrachequesne
respectabiggle
asked this question in
Q&A
-
My app currently connects to the same external ws URL twice, once from server.js and once from client.js. This seems inefficient.
Is it possible to pass an externally-sourced data stream between server.js and client.js with socket.io? This StackOverflow thread implies that this is not possible with socket.io. Am I misreading the thread, or is it mistaken? Thank you. |
Beta Was this translation helpful? Give feedback.
Answered by
darrachequesne
Oct 8, 2022
Replies: 1 comment 1 reply
-
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? |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
respectabiggle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! You should be able to pipe the events from the source to the Socket.IO connection:
Does it suit your use case?