connect 2 websockets ws and socket.io

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



connect 2 websockets ws and socket.io



I'm getting data from a web page with websocket but I need to deploy it with socketIO to client. In my server.js client connects with socketio well but after that data(line1 line2) can't comes properly, always I need to restart server 2-3 times.Then it comes.



Here is my declerations


var server = require('http').Server(app);
var io = require('socket.io')(server);
const WebSocket = require('ws')
const wss = new WebSocket('gettingDataAdress');

io.on('connection', function (socket) {
console.log("client connected");


wss.on('open', () =>
console.log("send processing");
//line1
)
wss.on('message', () =>
console.log("getting message processing");
//line2
)



After restarting my server.js 2-3 times it can comes to line1 and line2 , it can't directly.However whenever I comment the socketio Part(I mean only websocket working) it works perfect.How can I do that ? Thank you





I think you are mixing up socket.io with plain web sockets, just use one, in your case socket.io
– Alexandru Olaru
Mar 22 at 6:05





Could you please show all the code include the declaration of wss ?
– edkeveked
Mar 22 at 6:07


wss





@AlexandruOlaru I have to use websocket to get data and post with socketio
– gnncrdm
Mar 22 at 6:11





@edkeveked I'm editting code you can check it
– gnncrdm
Mar 22 at 6:12





wss and socket.io runs on different ports?
– Artur P.
Mar 22 at 6:12




2 Answers
2



You are using two different websockets ws and socket.io. Use only one to connect to the client and subscribe to the coming messages


ws


socket.io


io.on('connection', function (socket) {
console.log("client connected");


socket.on('open', () =>
console.log("send processing");
//line1
)
socket.on('message', () =>
console.log("getting message processing");
//line2
)


const WebSocket = require('ws');

const ws = new WebSocket('url');

ws.on('open', () =>
//do processing
);

ws.on('message', () =>
//do processing
);





But the data provider gives the data with websocket I have to connect them ?Can I do it with socket io because provider links like ws://http
– gnncrdm
Mar 22 at 6:16





Definitely, you can. You simply need to use the correct url to subscribe with socket.io
– edkeveked
Mar 22 at 6:24





Do I need to define 2, const wss = new WebSocket("''); because one for api provider one for my client ?
– gnncrdm
Mar 22 at 6:29





Yes you can do that since they won't listen using the same url
– edkeveked
Mar 22 at 6:32





Thank you whenever i try i ll share the result with you thank you
– gnncrdm
Mar 22 at 6:33



wanted to write a comment but had no reputation to write, so decide write on here. sorry!



If you want to subscribe, rather use socket.io-client. However, socket-io itself is not a proper library to subscribe wss.


socket.io-client


socket-io


wss



https://github.com/socketio/socket.io-client/issues/1208



actually, Socket.IO is not a WebSocket implementation, it has its own protocol which may use a WebSocket connection to transmit data
-darrachequesne-(most contributor of socket-io client library)



so If you get data from wss page, then use ws library and spread it by socket-io. I believe that what you are doing is pretty fine. might need to fix a bit though.



Connecting to GDAX websocket api using socket.io with webpack
there is similar question to get data from ws.





This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– rsjaffe
Aug 6 at 3:20





@rsjaffe I already mentioned
– Hongssi Ice
Aug 6 at 3:35






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard