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.)

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

5 Answers 5

3

Use the same version of socket server and client

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

5 Comments

I have the same version on both sides. But getting the cors error and xhr poll error
You need to add allow cors in your node js code. Please let me know if you need any help.
I already did that. import SocketIO from "socket.io"; import cors from "cors"; app.use(cors());export const io = SocketIO(server); io.on("connection", function (socket) { console.log("connection succeed"); io.emit("hello", { id: socket.id, }); });
@JaynaTanawala can you try to use the below code app.use(cors({origin: '*'}));
yes, I tried this. But showing me the same error
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.

1 Comment

4 years later, and you just saved my day.
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.

Comments

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 [email protected]". in my client js file I use socket.io-client like that

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

Comments

0

My problem was about cors. I added cors: {origin: {*} and it worked. I don't really what it does but it's a security thing.

const httpServer = require("http").createServer(); const io = require("socket.io")(httpServer, { cors: { origin: "*" }, }); io.on("connection", (socket) => { console.log("user connected"); }); httpServer.listen(8080, () => console.log("listenting")); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.