Socket.io: Socket events getting too much time to fire

Created on 17 Feb 2018  路  21Comments  路  Source: socketio/socket.io

I am using socket.io for my chat app. (HTML + JS & node js). It's working fine and great when one client connects to it.

But when another window is opened to start the chat, the chat becomes very slow. When three or four users connect to the chat at the same time, it's getting even slower such that, it looks like, it's almost freezed.

FYI, throughout the application, I have used socket.emit() (with & without callbacks). My requirement is, receive a message from the client, respond to the client. It's no group chat or anything. The user will chat with a bot (server). Said that, the message sent by the user (to the server) will not be sent to any other users. So, I fond that socket.emit() is the right way to deal with in this case.

Am I getting it wrong? Should I use some other way for emitting?

Note:-
The application works perfectly when running inside the enide studio with the inbuilt node js server. But, when deployed, the events are extremely slow.

Most helpful comment

Also see https://github.com/socketio/socket.io/issues/3100 .
A workaround is to change the wsEngine to ws in the options:
const io = require('socket.io')(httpServer, { wsEngine: 'ws' });

All 21 comments

UPDATE:-

I tried initiating a chat in chrome and other chat in IE. Same problem. So, the problem is not from the client end.

Then tried to join a different room for every user, then tried joining every user to the same room, then avoided joining a room (removed socket.join(room) ).

Nothing did a change. Still very much slow.

Could you provide the following please:

Setup

  • OS:
  • browser:
  • socket.io version:

Does it work properly with https://github.com/darrachequesne/socket.io-chat?

Cloning https://github.com/socketio/chat-example works well, but replacing the version with "^2.0.4" (and changing the cdn path to /socket.io/socket.io.js) makes it slow.

The data frames are still emitted immediately browser site (according to chrome using websockets as transport). but on server side socket.on('chat message' is delayed, like 3 seconds. When sending more messages client side the first one will emit an event on the server but the last one still takes 3 seconds.

Hi guys ! I'm having a similar issue.

When only 1 client socket is connected to the server, everything is fine.
When 2 or more client sockets are connected, The 'data' events are slow as hell (sometimes more than 3s whereas the client/server are both on the same machine : localhost).

After some digging, here is what I discovered :

OS : windows 10

Current npm modules versions :

  • socket.io : 2.0.4
    -> |__ engine.io : 3.1.5
    -> ->|__ uws : 9.14.0

When I manually came back to a version that doesn't have the issue :

  • socket.io : 2.0.4
    -> |__ engine.io : 3.1.4
    -> -> |_ uws : 0.14.5

To conclude : the problem comes from uws 9.14.0 (uws 0.14.5 is fine)

As i'm using socket.io directly (and not engine.io or uws) I reported the issue to your package and not to engine.io or uws.

I think the real culprit here is engine.io who bumped a "patch" version where they upgrade uws 0.14.5 to 9.14.0 without checking if uws 9.14.0 works correctly first...

Also see https://github.com/socketio/socket.io/issues/3100 .
A workaround is to change the wsEngine to ws in the options:
const io = require('socket.io')(httpServer, { wsEngine: 'ws' });

Thanks luffs, your solution fixed my problem. Same as rolandstarke : the chat example worked perfectly with sources but there was latency with socket.io 2.0.4 (Windows 10), even on the first emit.

Note: due to these kind of issues, ws will be restored as default wsEngine in socket.io 2.1.0

I have the same issue and waiting eager for 2.1.0. Do you have some date for release?

I am also having this issue using Socket.IO and Node.js for Unity. I have updated to the latest version of Node and updated the express and socket.io packages for my server.

I get a ReferenceError: httpServer is not defined if I add (httpServer, { wsEngine: 'ws' })

@GermanBluefox I'm waiting for some input on https://github.com/socketio/socket.io/pull/3195

@michaelrdnk httpServer comes from require('http').createServer().

@darrachequesne

Still getting it, what am I doing wrong here:

var express = require('express');
var app = express();

var server = require('http').createServer(app);
var io = require('socket.io').listen(server)(httpServer, { wsEngine: 'ws' });

app.set('port', process.env.PORT || 80);

server.listen(app.get('port'), function () {
    console.log('----- SERVER STARTED -----');
});

Without (httpServer, { wsEngine: 'ws' }) the server works fine.

@michaelrdnk

var express = require('express');
var app = express();

var server = require('http').createServer(app);
var io = require('socket.io')(server, { wsEngine: 'ws' });

app.set('port', process.env.PORT || 80);

server.listen(app.get('port'), function () {
    console.log('----- SERVER STARTED -----');
});

Thank you all, my problem has also been solved

wsEngine: 'ws' doesn't solve mine, the event still late

i even created a clean new project with this code
still have the same problem

const sio = require('socket.io');

const PORT = 3000;

const io = new sio({
    wsEngine: 'ws'
});
io.listen(PORT);
console.log("Server started, listening on port "+PORT);

io.on('connection', function(socket) {
    socket.on('test', function(data, callback){
        let test = data["test"];
        console.log(test);
    });


    socket.on('disconnect', function(reason){
        console.log(reason);
    });
});

i have another project with the same socket.io version 2.0.4 installed kinda long ago but doesn't have this problem

@GermanBluefox Thanks that worked for me and solved this whole slow response issue!

Thanks @luffs, my problem has also been solved.

I was having a similar issue. I fixed it by changing the connection from a proxy to the server directly:
before:
client side

const socket = openSocket(`/`)

after

const socket = openSocket(`http://localhost:3030`)

I'm doing this using ReactJs and NodeJs.
I'am facing the same problem here. When I open my site in two or more tabs then my site goes slow.
Client Side Code is Bellow:

   import React, { Component } from "react";
   import axios from 'axios';
   import $ from "jquery";
   import io from 'socket.io-client';
   import DatePicker from 'react-datepicker';
   import moment from 'moment';
   import {
      BrowserRouter as Router,
      Route,
      Link,
      Switch,
      Redirect,
      withRouter
    } from "react-router-dom";

  import 'react-datepicker/dist/react-datepicker.css';
  var formurlencoded = require('form-urlencoded');
  const socket = io('http://localhost:3001');
  class Abc extends Component {
       constructor(props) {
            super(props);
        }
      componentDidMount() {
         var user = localStorage.getItem("loggedInUser");
          socket.emit('set-user', user);
         user = JSON.parse(user);
         console.log(user);
   }

 }

My Backend Code is bellow:

 var express = require('express')
 , validate = require('express-validation')
 , bodyParser = require('body-parser')
 app = express();

 var path = require("path");
 var mysql = require("mysql");
 var http = require('http').Server(app);
 var io = require('socket.io')(http);
 var bs = require("black-scholes");
 var greeks = require("greeks");
 var md5 = require('md5');
 var jwt = require('jwt-express');
 var config = require('./config');

 var router = express.Router();
 var MySQLEvents = require('mysql-events');
 var dsn = {
    host: config.dbconfig.host,
    user: config.dbconfig.user,
    password: config.dbconfig.password,
 };
 var mysqlEventWatcher = MySQLEvents(dsn);

 io.on('connection',function(socket){
     console.log('We have user connected !');
     socket.on('set-user', function(user) {
     socket.user = user;
 });
 });

OS: Windows 7
socket.io version: ^2.0.4
Browser: Google Chrome and Mozilla

Same issue here
client side gets freezed after sending 3-4 messages
The issue came after implementing XYZ is typing functionality.

OS: Windows 10
browser: Chrome (80)
socket.io version: 2.2.0

actually setup is 5 minutes to poll and get the data from the browser

in the network every 28 seconds its polling please help me on that

Which part of the code should i change inorder to ensure faster response times.
I am using flask-socketio , is it the server side code or the client side code that i should edit ?
I have deployed my site on pythonAnywhere.

I am using

socket = io.connect('http://'+document.domain+':'+location.port);

to connect to socket.

Was this page helpful?
0 / 5 - 0 ratings