1

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

edkeveked
  • 17,989
  • 10
  • 55
  • 93
erdemgunenc
  • 969
  • 3
  • 14
  • 25

2 Answers2

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

Only 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
   })

Only ws

const WebSocket = require('ws');

const ws = new WebSocket('url');

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

ws.on('message', () => {
  //do processing
});
edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • 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 – erdemgunenc Mar 22 '18 at 06:16
  • Definitely, you can. You simply need to use the correct url to subscribe with socket.io – edkeveked Mar 22 '18 at 06:24
  • Do I need to define 2, const wss = new WebSocket("''); because one for api provider one for my client ? – erdemgunenc Mar 22 '18 at 06:29
  • Yes you can do that since they won't listen using the same url – edkeveked Mar 22 '18 at 06:32
  • Thank you whenever i try i ll share the result with you thank you – erdemgunenc Mar 22 '18 at 06:33
  • By the way in my server.js const wss = new WebSocket('wss://http://localhost:3000'); How the url shoud be for the client ? – erdemgunenc Mar 22 '18 at 06:37
  • I tried but I guess I'm wrong with url and port, should the port same with nodejs server port like 3000 ? Because it says Error: socket hang up const wss = new WebSocket('wss://provider'); const wss2 = new WebSocket('ws://localhost:3000'); – erdemgunenc Mar 22 '18 at 07:18
0

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.

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.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47