Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io automatic timeout and transport error

I am building a chat application using node-Js(0.10.33), socket.io server and client(both 1.2.1) and express(3.18.4). I am using reverse proxy to hide the url(example.com:8083). I have been facing an issue with automatic timeout and causing transport error and transport close respectively. Below are my files that I am using: server.js

var app = require('express')();
var http = require('http').createServer(app);
var io = chat = require('socket.io')(http, {
  'polling duration' : 10,
  'heartbeat interval': 25,
  'heartbeat timeout': 99999,
  'close timeout': 86400,
  'transports' : ["polling", "websocket"]
});

var configurations = {};

var configurations = {
  config: fs.readFileSync("./config.json").toString()
};
var configData = JSON.parse(configurations.config);
// Configure Application IP and PORTS.
app.set('port', configData.PORT || 8080);
app.set('hostName', configData.HOST || "127.0.0.1");

// Connect to the server and listen.
http.listen(app.get('port'), app.get('hostName'), function(){
  console.log('Express server listening on  Host: ' + app.get('hostName') + ' and port ' + app.get('port'));
});

// Server connection to chat system.
chat.on('connection', function (socket) {
  // New user Join to chat.
  socket.on('new user', function (userData) {
    //New user joins
  });

  // User Left the chat.
  socket.on('disconnect', function (data) {
    // Page refresh
  });

  // User removed from the private chat list by the initiator.
  socket.on('remove user', function (userData) {
    // User is removed forcefully
  });

  // User is typing a message. Send this information to client.
  socket.on("typing", function(data) {
    // User is typing a message.
  });

  // On new message receive.
  socket.on('new message', function (message, isInstructor) {
    // When the user posts a new message.
  });

  // On message deletion.
  socket.on('delete message', function (msgBlockId) {
    // Upon deleting a message
  });

  // Debug statements in time of reconnection.
  socket.on('connect_error', function (data) {
    console.log('connect_error');
    console.log(data);
  });
  socket.on('connect_timeout', function (data) {
    console.log('connect_timeout');
    console.log(data);
  });
  socket.on('reconnect_error', function (data) {
    console.log('reconnect_error');
    console.log(data);
  });

});

Client.js

var timeout = undefined;
var $timeOutVal = undefined;
var hostName = Drupal.settings.config;
var max_socket_reconnects = '';

// Socket configurations;
var socket = io.connect('http://' + hostName, {
  'path': '/chat-connector',
  'forceNew': true,
  'reconnection': true,
  'reconnectionDelay': 1000,
  'reconnectionDelayMax' : 5000,
  'reconnectionAttempts': 5
});
//tell socket.io to never give up :)
socket.on('error', function(exception){
  console.log("Error occ");
  console.log(exception);
  socket.socket.connect();
});

(function ($) {
  // Calling events.
}(jQuery));


/**
 * Functions to be implemented upon socket connection.
 */
socket.on('connect', function (data) {

  socket.on('usernames', function(data) {
    // Updating user list upon addition of user
  });

  socket.on('broadcast message', function (data) {
    // Posting messages.
  });

  socket.on('updated usernames', function (data) {
    // Updating user list upon deletion of user
  });

  socket.on('notify', function (data) {
    // Posting notifiaction messages
  });

  socket.on('updated messages', function (data) {
    // Updating message board
  });

  socket.on('post remove user', function (data) {
    // Addressing an event
  });

  socket.on("reconnecting", function(delay, attempt) {
    if (attempt === max_socket_reconnects) {
      setTimeout(function(){ socket.socket.reconnect(); }, 5000);
      return console.log("Failed to reconnect. Lets try that again in 5 seconds.");
    }
  });

});


/**
 * Function to set a timeout for chat typing message display toggle.
 */
function timeoutFunction() {
  typing = false;
  socket.emit("typing", false);
}

I am getting unwanted ping timeout and after certain ping timeouts the transport error is occurring which is causing the clients to drop from the chatroom. Also checking via console, I am getting the following error:

WebSocket connection to 'ws://example.com/chat-connector/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

But this error is intermittent. Please suggest so that I could resolve this issue.

like image 904
GRISHMA SINHA Avatar asked Nov 01 '22 12:11

GRISHMA SINHA


1 Answers

  1. Verify if Client and Server have same socket.io version
  2. Verify the destined ports are not blocked
like image 86
yunas Avatar answered Nov 15 '22 13:11

yunas