Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js and socket.io. transport type configuration for websocket?

This question concerns socket.io versions < 0.9.x. Newer versions have different transports and methods of setting transports.

I test node js and socket.io in two week. when I began I get the problem from socket.send(message) function in client. I can't send any message to the server. But I still can receive messages from the server. I solved this problem when I found the configure transport of server side:

socket.set('transports',[
   'xhr-polling'
  , 'jsonp-polling'
]);

Everything good. Now I can send messages to the server as well. But I still have a question why I have to configure transport. Default socket.io use websocket transport setting like this:

socket.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
]);

so it uses websocket at first, not xhr-polling. But the server cannot receive any messages sent from the client when using socket.send(msg) even socket.emit(...).

So the problem is: what is not supporting websocket here? browser or node.js ... I'm sorry but I searched so many pages from google and I haven't found an answer for this.

I use node.js version 0.8.16, socket.io version 0.9.13 and newest browsers: chrome, firefox, opera

I want to use websocket not xhr-polling.

like image 587
Rai Blaze Avatar asked Jan 15 '13 08:01

Rai Blaze


1 Answers

That's odd because even if websockets are not supported by your server configuration, socket.io will select the next best available method (in your case xhr-polling). Actually, you shouldn't even need to set those transports as socket.io will try to use 'websocket' as a primary method by default. This may indicate some other problem, possibly with your code?

What is not supporting websockets is definitely not the browsers you're using nor node.js of course. This will depend on your server setup.

First check:

  1. The port you're listening to is open in your firewall
  2. Your webserver supports websockets. If you're using Apache and proxing your request to an internal IP:PORT, websocket will not work unless you install something like apache-websocket or pywebsocket

What finally solved my issue was to disable Apache listening on port 80 and having node.js listening on that port. Here's the answer on SO that helped me: https://stackoverflow.com/a/7640966/2347777

like image 199
Gianluca Pinoci Avatar answered Sep 27 '22 18:09

Gianluca Pinoci