Socket.io and modern browsers do not work

I'm having some weird issues with socket.io and modern browsers. Surprisingly, IE9 works just fine because backing up to flashsocket seems to work better.

On my server (with express)

var io = socketio.listen(server.listen(8080)); io.configure('production', function(){ console.log("Server in production mode"); io.enable('browser client minification'); // send minified client io.enable('browser client etag'); // apply etag caching logic based on version number io.enable('browser client gzip'); // gzip the file io.set('log level', 1); // reduce logging io.set('transports', [ // enable all transports (optional if you want flashsocket) 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling' ]); }); 

In the browser, I see on the "Network" tab (in Chrome) that websocket is installed and enters the 101 Switching Protocols in standby mode. After that, xhr polling and jsonp polling appear (what happened with flashsocket?)

The worst part is that the information does not go back and forth. I have this when connecting:

 io.sockets.on('connection', function (socket) { // If someone new comes, it will notified of the current status of the application console.log('Someone connected'); app.sendCurrentStatus(socket.id); io.sockets.emit('currentStatus', {'connected': true); }); 

And on the client:

 socket.on('currentStatus', function (data){ console.log(data) }); 

However, I can see this log only when the server is turned off, which starts with:

 NODE_ENV=production node server.js 

What am I doing wrong?

+6
source share
2 answers

Finally, after I really hit my head against the wall, I decided to test in several environments to see if this was a Firewall problem, since the machine is behind several.

It turned out that I had no problem, so I checked Antivirus (Trend Micro) and after disabling Chrome / Firefox I was able to do my magic.

Moral of history

In addition to what it says here - Socket.IO and firewall software - whenever you encounter a problem that no one seems to be on the Internet (i.e., neither the socket.io group has logged into github) , it is caused by your antivirus. They are evil. Sometimes.

+9
source

You just need to listen to the socket in the application itself.
Also, I never need to do all the server-side configuration with the socket you are doing - socket.io should work out of the box in most browsers without doing this. At first I would try without tuning. In addition, on the server, you must emit from the socket that is passed to the callback function, and not from io.sockets.on.

 var io = socketio.listen(app); io.sockets.on('connection', function (socket) { // If someone new comes, it will notified of the current status of the application console.log('Someone connected'); app.sendCurrentStatus(socket.id); socket.emit('currentStatus', {'connected': true); }); 

on the client, you need to connect first:

 var socket = io.connect(); socket.on('currentStatus', function (data){ console.log(data) }); 

If you want to see an example of two-way communication using socket.io, check out my Nodio app.

Server side: https://github.com/oveddan/Nodio/blob/master/lib/Utils.js

And the client side: https://github.com/oveddan/Nodio/blob/master/public/javascripts/Instruments.js

+1
source

Source: https://habr.com/ru/post/921304/


All Articles