Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My task is to implement a Node.Js server ("my server") that will continuously read-in messages which are being emitted from several different websocket connections ("source servers", different URLs) and then process these messages somehow.

I was successful in the case of ws (or wss) connection, e.g. using the ws package in this case:
JavaScript
var WebSocket = require('ws'),
  ws = new WebSocket('wss://ws.pusherapp.com:443/app/__SECRET_API_CODE__');

ws.on('message', function(message) {
  console.log(message);
});


But the same approach does not work if the source server uses socket.io library, e.g. http://io.weplay.io (taken from http://socket.io/demos/weplay/).

Could somebody provide me a hint - I will appreciate any programming language - how to consume websocket messages from different source servers in my server application? Preferrably in an universal way, without depending on the concrete implementation of WebSocket protocol.

Thank you very much!
Posted

1 solution

Package socket.io-client solves the problem :-)
JavaScript
var socket = require('socket.io-client')('http://io.weplay.io');
 
socket.on('connect', function(){
  console.log('Connected.');
});
 
socket.on('message', function(data){
  console.log(data);
});

I could not solve it for several hours because of typo in the code. Uuuuf.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900