125
votes

I have a socket.io server running and a matching webpage with a socket.io.js client. All works fine.

But, I am wondering if it is possible, on another machine, to run a separate node.js application which would act as a client and connect to the mentioned socket.io server?

6
How do I view the response from socket.emit()?codecowboy
Go through the github.com/LearnBoost/socket.io-client documentation, I am sure it is there. It has been a while, so I do not remember, sorry...Predrag Stojadinović
It isn't working if io.connect function is called as you mentioned. It should be called as: socket = io.connect('http://localhost:1337');ceremcem

6 Answers

74
votes

That should be possible using Socket.IO-client: https://github.com/LearnBoost/socket.io-client

47
votes

Adding in example for solution given earlier. By using socket.io-client https://github.com/socketio/socket.io-client

Client Side:

//client.js
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', {reconnect: true});

// Add a connect listener
socket.on('connect', function (socket) {
    console.log('Connected!');
});
socket.emit('CH01', 'me', 'test msg');

Server Side :

//server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

  socket.on('CH01', function (from, msg) {
    console.log('MSG', from, ' saying ', msg);
  });

});

http.listen(3000, function () {
  console.log('listening on *:3000');
});

Run :

Open 2 console and run node server.js and node client.js

11
votes

After installing socket.io-client:

npm install socket.io-client

This is how the client code looks like:

var io = require('socket.io-client'),
socket = io.connect('http://localhost', {
    port: 1337,
    reconnect: true
});
socket.on('connect', function () { console.log("socket connected"); });
socket.emit('private message', { user: 'me', msg: 'whazzzup?' });

Thanks alessioalex.

0
votes

Yes you can use any client as long as it is supported by socket.io. No matter whether its node, java, android or swift. All you have to do is install the client package of socket.io.

0
votes

Client side code: I had a requirement where my nodejs webserver should work as both server as well as client, so i added below code when i need it as client, It should work fine, i am using it and working fine for me!!!

const socket = require('socket.io-client')('http://192.168.0.8:5000', {
            reconnection: true,
            reconnectionDelay: 10000
          });
    
        socket.on('connect', (data) => {
            console.log('Connected to Socket');
        });
        
        socket.on('event_name', (data) => {
            console.log("-----------------received event data from the socket io server");
        });
    
        //either 'io server disconnect' or 'io client disconnect'
        socket.on('disconnect', (reason) => {
            console.log("client disconnected");
            if (reason === 'io server disconnect') {
              // the disconnection was initiated by the server, you need to reconnect manually
              console.log("server disconnected the client, trying to reconnect");
              socket.connect();
            }else{
                console.log("trying to reconnect again with server");
            }
            // else the socket will automatically try to reconnect
          });
    
        socket.on('error', (error) => {
            console.log(error);
        });
0
votes

something like this worked for me

const WebSocket = require('ws');
const ccStreamer = new WebSocket('wss://somthing.com');

ccStreamer.on('open', function open() {
  var subRequest = {
    "action": "SubAdd",
    "subs": [""]
  };
  ccStreamer.send(JSON.stringify(subRequest));
});

ccStreamer.on('message', function incoming(data) {
  console.log(data);
});