135

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?

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Predrag Stojadinović
  • 3,439
  • 6
  • 34
  • 52

7 Answers7

78

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

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 8
    Uhm, I might be mistaking, but this looks like the client that runs within the browser. What I need is a stand-alone node.js client. – Predrag Stojadinović May 22 '12 at 15:09
  • I haven't checked recently, but in Node 0.4.x this worked on the server too (I have actually implemented this in a past project). – alessioalex May 22 '12 at 18:59
  • 2
    I'm glad it worked for you! Btw, it's better to put your working example on the question rather than in a separate answer. – alessioalex May 23 '12 at 13:28
  • This didn't install correctly for me on windows 8 - i wrote a bug for it – B T Sep 20 '13 at 22:23
  • @PredragStojadinović : Can you please post your code? I want to connect one NodeJS sever to another. Can you help me out? Thanks. – Pritam Jan 19 '15 at 12:25
  • http://stackoverflow.com/questions/29108594/node-js-socket-io-client-is-not-connecting-to-socket-io-server – exilonX Mar 17 '15 at 21:34
60

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

Calamar
  • 1,547
  • 1
  • 13
  • 25
AzizSM
  • 6,199
  • 4
  • 42
  • 53
  • 3
    Awesome examples! One thing, on the client side, I don't believe the "socket" variable gets passed on the connection event. Maybe I'm wrong, but that seems to be the behavior I'm seeing with npm socket.io-client – Ryan S Aug 22 '16 at 04:05
  • Did you fix that yet @RyanS – Bombosonic Dec 25 '22 at 14:15
  • 1
    @Bombosonic yo! shoot, this was a while ago lol, I don't remember what I ended up doing, sorry! – Ryan S Jan 09 '23 at 22:59
14

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.

Federico Fusco
  • 545
  • 1
  • 5
  • 18
Predrag Stojadinović
  • 3,439
  • 6
  • 34
  • 52
  • can you emit without waiting for a socket connection? my emissions fail and when I look at the `socket.connected` property right before the `.emit()` it's `false` – ekkis Apr 15 '22 at 01:03
2
const io = require('socket.io-client');
const socket_url = "http://localhost:8081";

let socket = io.connect(socket_url);

socket.on('connect', function () {
    socket.emit("event_name", {});
});
0

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

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);
        });
Rohit Maurya
  • 154
  • 1
  • 1
  • 10
0

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