Socket.io auto timeout and transport error

I am creating a chat app using node-Js (0.10.33), socket.io server and client (both 1.2.1) and express (3.18.4). I am using a reverse proxy to hide the url (example.com:8083). I am facing an issue with auto timeout and transport and transport error. 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 there is a transport error causing clients to exit the chat. Also checking through the console I get the following error:

WebSocket connection from '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 can resolve this issue.

+3


source to share


1 answer


  • Check if client and server have the same socket.io version
  • Make sure the intended ports are not blocked.


+1


source







All Articles