Thanh Dao
Thanh Dao

Reputation: 1260

Why socketIO call too much connections?

I created my Socket server with Express, SocketIO and Redis

server.js

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

server.listen(8890, function (e) {
    console.log(e);
});
io.on('connection', function (socket) {
    console.log("new client connected");
    var redisClient = redis.createClient();
    redisClient.subscribe('message');

    redisClient.on("message", function(channel, message) {
        console.log("mew message in queue "+ message + "channel");
        socket.emit(channel, message);
    });

    socket.on('disconnect', function() {
        redisClient.quit();
    });

    socket.on('connect_error', function() {
        redisClient.quit();
    });
});

From command line, I run node server.js. Its worked.

I also created a html file to connect to that server. From Browser Console, I run io.connect('http://localhost:8890'). I got as the result
enter image description here

As I see, too much connections (requests). What happens? What wrong from my code?

Upvotes: 1

Views: 2934

Answers (1)

jfriend00
jfriend00

Reputation: 708146

You have mismatched client and server versions causing the initial connection to fail and the older client is dumb enough to just keep trying over and over again. If you are using 2.0.4 on the server, then you must use that version for the client too. If you serve the client version directly from your server with:

<script src="http://localhost:8890/socket.io/socket.io.js"></script>

Then, the socket.io server will automatically give you the right client version.

Or, you can manually link to the right version on your CDN such as https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js. But client and server versions MUST match.

The advantage of getting the client directly from your own server is that anytime you update your server version of socket.io, the client version will automatically be upgraded for you and kept in perfect sync since the matching client version is built into the server version.

Upvotes: 4

Related Questions