3

I am trying to connect two node.js servers using socket.io and socket.io-client. In both cases, I am using v0.9.16.

In my case, I call the servers the STREAM SERVER and the ACTIVITY SERVER where the activity server uses the socket.io-client module.

STREAM SERVER - (THE CODE ON THE SERVER THAT ACTS LIKE A SERVER)

var https = require('https');
var express = require('express');
var socket = require('socket.io');
var securePort = (process.env.LOCAL_HTTPS_PORT || 443);
var sslOptions = //CERTIFICATE
var socketIoConfiguration = //CONFIG VALUES
var app = express();
var server = https.createServer(sslOptions, app);
var io = socket.listen(server, socketIoConfiguration('activityToStream'));
io.sockets.on('connection', function (socket) {
    console.log('Activity server connected to stream server.');
});
server.listen(securePort);

ACTIVITY SERVER - (THE CODE ON THE SERVER THAT ACTS LIKE THE CLIENT)

var socketClientModule = require('socket.io-client');
var streamConnectionServer = 'https://165.225.144.273:443';
var activityToStreamSocket = socketClientModule.connect(streamConnectionServer);
activityToStreamSocket.on('connect', function(socket){
    console.log('Connected to Stream Server');
});

When I run this code, I don't get any message from either server. However, when I run this code from an HTML page served in Chrome, I see messages on the output of the Stream server:

<!DOCTYPE html>
<html>
<head>
    <title>
        Test
    </title>
    <script src="https://165.225.144.273/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('https://165.225.144.273');
        socket.on('news', function (data) {
            console.log(data);
            socket.emit('my other event', { my: 'data' });
        });
    </script>
</head>
<body>
    Hello World
</body>
</html>

STREAM SERVER MESSAGES WHEN CLIENT RUN IN CHROME

debug: client authorized
info: handshake authorized UaUDRsA3ZBTgdsiLDCrl
debug: setting request GET /socket.io/1/websocket/UaUDRsA3ZBTgdsiLDCrl
debug: set heartbeat interval for client UaUDRsA3ZBTgdsiLDCrl
debug: client authorized for 
debug: websocket writing 1::

So, it makes me think the issue is with my "ACTIVITY SERVER" running socket.io-client, but I cannot figure out why its not working at all (no error messages, etc.)

Justin Elkow
  • 2,833
  • 6
  • 28
  • 60
  • Try connecting to the SocketIo server with a plain WS lib: http://einaros.github.io/ws/ worked well for me. It also has a nifty utility lib called wscat. – jcollum Mar 30 '14 at 00:00
  • That is a low level module. Wouldn't that require me to implement my own heartbeat, disconnect/reconnect, etc? – Justin Elkow Mar 30 '14 at 00:51

4 Answers4

3

Use the same version of socket server and client

Lakin Mohapatra
  • 1,097
  • 11
  • 22
2

I had the same issue, but my solution was simple:

My code was wrong here:

var serverSocket = ioclient.connect('127.0.0.1:6000');

Then I wrote 'ws://' before '127.0.0.1', and it began to work.

var serverSocket = ioclient.connect('ws://127.0.0.1:6000');

That gave me a headache for hours.

SevN
  • 31
  • 2
0

I was able to get it to work using the following:

1) socket.io v 1.0.0-pre using this command

npm install git+https://github.com/LearnBoost/socket.io.git

2) scoket.io-client v 1.0.0-pre using this command

npm install git+https://github.com/LearnBoost/socket.io-client.git

3) I used an SSL certificate from a CA instead of self signed and then I used a DNS name instead of an IP address (see this on not using unsigned certificates with Socket.io)

Here's my server and client (server acting as client). Reference the Socket.io-client GitHub page.

SERVER CODE

var sslOptions = //CERTIFICATE
var app = require('express')();  // Express 3.x
var server = require('https').Server(sslOptions, app);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
    console.log('Connection from Activity Server');
    for (var i=0; i<100000; i++) {
        io.emit('event', i);
    }
});
server.listen(443);

CLIENT CODE (SERVER ACTING AS CLIENT)

var now = Date.now();
var socket = require('socket.io-client')('https://example.com:443');
socket.on('connect', function(){
    console.log('Connected to Stream Server');
    socket.on('event', function(data){
        console.log('Ping ' + data);
        console.log(now + '   ' + Date.now());
    });
});

Using this Socket.io code, I was able to get over 13,000 messages per second. This was done on two Joyent servers in the same data center (Memory 256 MB, CPUs 0.125 and bursting, Network Up to 10 Gbit/s).

When doing more of a "Ping Pong" style messaging, I was able to get about 650 messages per second.

Note the performance numbers are just a datapoint for reference, I didn't do any tweaking, heaving loading, etc.

Community
  • 1
  • 1
Justin Elkow
  • 2,833
  • 6
  • 28
  • 60
0

I got the same situation but I fix this by switching the socketio-client version. I got socketio version 2.3. but I got socketio-client version 3.1. I changed the version of socketio client by using "npm install socket.io-client@2.3". in my client js file I use socket.io-client like that

const io = require('socket.io-client');
const socket = io('ws://localhost:3000',{
  
});