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?

3
  • 2
    How do I view the response from socket.emit()? Commented Jan 6, 2014 at 12:10
  • 1
    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... Commented Feb 12, 2014 at 10:46
  • 6
    It isn't working if io.connect function is called as you mentioned. It should be called as: socket = io.connect('http://localhost:1337'); Commented Nov 22, 2014 at 19:16

7 Answers 7

78

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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).
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.
This didn't install correctly for me on windows 8 - i wrote a bug for it
@PredragStojadinović : Can you please post your code? I want to connect one NodeJS sever to another. Can you help me out? Thanks.
|
62

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

3 Comments

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
Did you fix that yet @RyanS
@Bombosonic yo! shoot, this was a while ago lol, I don't remember what I ended up doing, sorry!
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.

1 Comment

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
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", {});
});

Comments

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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.