socket.io websocket connection invalid (latest chrome) - node.js

After installing node.js, I followed this tutorial to start a simple chat server. It was very easy to setup and is working, but I have noticed two problems:
1.) I am getting this warning from socket.io
info - socket.io started
debug - served static /socket.io.js
debug - client authorized
info - handshake authorized 1385647068766475337
debug - setting request GET /socket.io/1/websocket/1385647068766475337
debug - set heartbeat interval for client 1385647068766475337
warn - websocket connection invalid
This doesn't make sense to me because I didn't touch anything with socket.io and I am using the latest chrome version (23) which I know supports websockets (I am able to successfully connect to them with PHP-Websockets). It continues to use XHR instead, but I am really interested in getting the Websocket functionality working.
2.) When I go to localhost:8080 to connect to the chat server, it takes around 7-8 seconds for it to prompt me for my name and actually connect me to the server. I have a feeling this may be because it is reverting to XHR, but I don't really know much about it so I can't say. Any thoughts?

I saw this behaviour when using an older version of socket.io with later chrome builds (and other browsers also). It would timeout then fallback to xhr polling. To check your version of the socket.io library you are using, at your shell (linux/unix) type:
npm ls| grep socket.io
And it should tell you the version. The latest at this time is 0.9.13, which works.
If you are running the tutorial from http://psitsmike.com note that the package.json file hardcodes an older version of socket.io which doesn't work with the latest browsers.
Hope this helps.

I too was facing similar issues.
Your case -
Try deleting your cookies, sometimes the xhr-polling option once connected successfully is saved to cookies and reused every next time. Similar question answered here
Also debug - served static /socket.io.js sometimes come when the socket.io file is referred incorrectly inside the html or jade template file. Try correcting the script src link in case it wrong. It should be something like - var socket = io.connect('http://localhost:3000'); OR you can also try removing the link all together like this - var socket = io.connect();
Hope it helps.

I had the exact same issue. this might be old. but My setup is on digitalocean. what happens is that usually people who run node apps on one server use nginx for the port listen and node under its own dedicated port. nginx did not forward the websocket port to the node, only port 80 which nodejs was not initialised to begin with.
http://nginx.org/en/docs/http/websocket.html
You need to make sure your nginx is configured properly for this.
I just added the port to the io.connect directly connecting to the node server and avoiding nginx.

Related

Error during WebSocket handshake: 'Connection' header is missing

I'm completely new to Node, and even newer to Socket.io. I just got my first chat application online at spaiglas.com, which works despite an error in the developer console that is fairly straightforward...
WebSocket connection to 'ws://example.com/...' failed
: Error during WebSocket handshake
: 'Connection' header is missing
Here is a codepen with all of my code. I've uploaded it to my hosting provider, which is using Passenger via cPanel on an Apache server in a Node 9.11.2 environment.
From what I've been reading, the 'Connection' headers can't be set using AJAX, which is supposedly a security risk, but instead is handled by the server? Is it possible to manually set this header?
I have noticed by looking at the headers that early on usually one of them always successfully switches/upgrades the protocol to WebSockets, but then the platform always seems to revert back to HTTPS/polling. In the case below, which I observed just now, there were actually 2 out of now 67 instances whereby it successfully switched to and used the WebSockets protocol.
You need to initialize socket.io before you start your HTTP server that is listening on port 80. In the docs it is...
var http = require('http').Server(app);
var io = require('socket.io')(http);
And then later, you call http.listen(3000, ...). This will start the socket.
On the client, your path is also incorrect. Websockets is a protocol (ws://somedomain.com), just like HTTP. When you connect via http:// that is a different protocol. I would either not pass it anything, or use namespaces which you can find in the socket.io documentation.
Socket.io reverts to long polling and sometimes doesn't even use websockets (which is partly why it is generally slower than libraries like ws on npm). Because of that, passing http:// into the io(...) on the client may not be wrong with their library, but I would remove it anyway and do my above suggestion.

How to serve policy file from Socket.IO/Node.js

I've created a Socket.IO server which pushes listing details to connected clients. Over HTTP in a browser, everything works as intended using web sockets, however one of the client types that will consume the service is strictly Flash (not socket fallback, just straight up Flash). We are using the AS3 Web Socket Client library.
When the Flash client (built by another team) tries to connect to the server, it's getting a security sandbox violation. I'm aware this has to do with the policy file serving from the Socket.IO server, but I'm at a loss as to exactly how to serve up this file. When I telnet 127.0.0.1 10843 or telnet 127.0.0.1 843 I get no response. If I recall, in past projects we've been able telnet to the policy file server to get the response. This would be ideal to help me verify that it's being served.
I understand that the default port is 10843, and have also tried setting to 843.
Here's what I have:
Push service running on localhost:3000
Browser clients can connect and join channels no problem
I have a crossdomain.xml file in root directory (not sure if this is needed or not)
I have the following settings pertaining to the policy file:
// Socket Setup
io.set('transports', ['websocket','flashsocket']);
io.set('flashPolicyServer', true);
io.set('flash policy port', 843);
An aside (maybe), with these settings I'm also getting an error on starting the service (though it still starts):
Option flashPolicyServer is not valid. Please refer to the README.
Option flash policy port is not valid. Please refer to the README.
I'm at a loss. Hopefully not a duplicate; I searched high and low and believe I'm missing something minor. Any recommendations?
It seems that Socket.IO developers decided to get rid of some transports, and thus since 1.0 there is no Flash transport support. This is a bit confusing because on the new docs there is simply no mentioning of Flash transport presence at all.
In Socket.IO 0.9 you could find WebSocketMain.swf and WebSocketMainInsecure.swf files somewhere inside socket.io folder. In 1.* there are no any files with .swf extension.
So if you need Flash support you should install Socket.IO 0.9 with this command:
npm install socket.io#0.9
According to the socket.io documentation, the flash policy server defaults to true when the flashsocket transport is enabled. This means that the flashPolicyServer (which should be flash policy server) option is unnecessary. Try setting the port before setting the transports as well... like this.
io.set('flash policy port', 843);
io.set('transports', [ 'websocket', 'flashsocket' ])
This ensures that when the flash socket server starts up, it starts with that port, rather than starting, getting killed and then restarted with the new port. Also, since 843 is a root port, make sure it has permission to use that port.
edit
It seems as though socket.io dropped support for flash outright. The docs on their github are for 0.9.0. Which is confusing. There is an alternative that might be useful although I have not tried it. FlashSocket.io should help facilitate your needs.

Node.js and Socket.IO Not Responding in Some Servers

I have simple Node.js and Socket.IO app running on Nodejitsu. The App is running perfectly from home and some other Internet services but not working from my work!
Here is the error Message which I am getting
The connection to ws://behseini-socketproject.jit.su/socket.io/1/websocket/QtVm6E79WwGLwBVjN7el was interrupted while the page was loading.
this.websocket = new Socket(this.prepareUrl() + query);
socket.io.js (line 2371)
GET http://behseini-socketproject.jit.su/socket.io/1/?t=1370531712581 200 OK
535ms
socket.io.js (line 1659)
ParamsHeadersResponse
fPtjDNnugmPPXKbnN7ep:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
Can you please let me know why this is happening? is this kind of Firewall issue?
Thanks
After talking with you on the #nodejitsu IRC channel, it seems that this may indeed be a problem with your work proxy/firewall.
As was suggested to you in the chat last night, you could always try to use secure websocket connections (wss rather than ws) to see if that will allow your connections to pass through.
If that doesn't work, you would need to send your connection outside of your work's connection. For instance, proxying into a computer at your home and using your home network while you're at work and want to use websockets.

"Unexpected response code: 503" in Chrome. Perhaps having to do with socket.io on heroku?

I am using socket.io with node.js on Heroku. Everything seems to work. However, in the Chrome dev bar, I get Unexpected response code: 503. Does anyone know what this could mean? Should I be concerned about it? Will it take up memory in the browser? And if so, can I suppress it?
Heroku "doesn't support" websockets on the Cedar stack yet (no word on when they will) Update: see below. They recommend adding the following code to your Socket.io implementation:
// assuming io is the Socket.IO server object
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
The reason for this is that Heroku adds a routing layer on top of your dynos. Your code will be executed on a server, but it's non-deterministic as to which server it will be. Thus, WebSockets can't send data to the "correct" server. Socket.IO will downgrade to long-polling, which keeps the connection alive through the routing layer, ensuring that the events will be executed on the correct server.
UPDATE: Heroku WebSockets is now in public beta! You can add WS support to your app by executing heroku labs:enable websockets -a myapp in your app directory.
For people coming from google in 2016.
Heroku websockets are now supported, no need to turn them on. WebSockets on Heroku
Make sure to check your logs on Heroku. I noticed the 503 error happen in chrome and after checking logs, turned out that my Heroku app had crashed. There was a bug in my code which caused it to not responding (not socket.io related at all)
Fixed the code and 503 went away!

nodejs socket.io with IIS Node

I have tried something with node.js in windows vista/IIS 7 using iis node. My idea is to use a server script on the asp .net mvc application to connect to another socket server and serve requests on a persistent fashion.
I downloaded the iisnode version at https://github.com/tjanczuk/iisnode for IIS 7 and able to run the basic 'hello world' http server pipe using http handler mappings in web.config and IIS modules configuration.
I am trying to use socket.io library from node.js. This works independently if I write a server and client. But fails when used with IIS node.
I am having problems going down further. Problems like
How to include the npm modules in the asp .net mvc project? I tried
putting the node_modules in the folder of node scripts, but that did
not help. Basically the require('socket.io') command works, but the
socket connection etc., simply fails.
How to have a socket
connection from client which will keep listening for updates from
server(like COMET) - As I said I am trying to use socket.io
Any body tried this before?
Reading back a few months ago, WebSocket support is not supported with Socket.IO under IIS, however long polling is.
This was a few months ago, and I'm running up against the same issue now and trying to resolve.
As Tomasz writes:
Please note that iisnode does not support websocket transport, but
using socket.io is still possible with other HTTP-based transports
like HTTP long polling:
io.configure(function() {
io.set('transports', ['xhr-polling']);
});
By the looks of it, as of Feb 23, 2012, this functionality is still not supported.
Do you need to go through IIS? Do you have the option of going with a pure Socket.IO/Node option, eliminating IISNode? If you need full WebSocket support with fallback capability, this looks like the only option, unless there are other suggestions?
This may or may not be related to your problem. I haven't done much with socket.io, however I was planing to do something very similar to what you're describing. When reading through the source for iisnode, I found the following code in cnodehttpmodule.cpp:
this->applicationManager->GetEventProvider()->Log(L"iisnode received a new http request", WINEVENT_LEVEL_INFO);
// reject websocket connections since iisnode does not support them
// http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17#page-17
PCSTR upgrade = pHttpContext->GetRequest()->GetHeader(HttpHeaderUpgrade, NULL);
ErrorIf(upgrade && 0 == strcmp("websocket", upgrade), ERROR_NOT_SUPPORTED);
It looks to me as though if "websocket" is included in the header of the request, it will be rejected. I would need to read up on the websocket protocol to better understand exactly what this means.
I'll be the first to admit that I don't know overly well how websockets differ from a long running request. As I understand it, however, socket.io will work on older browsers that do not support websockets.
I recommend you try to set the transport to just xhr-polling or jsonp-polling. It kinda defeats the cool factor of using node.js but it might help you reach a resolution to your problem.
If your socket.io web application is hosted in an IIS virtual directory, socket.io configuration must be modified compared to a self-hosted case. Please see http://tomasz.janczuk.org/2013/01/hosting-socketio-websocket-apps-in-iis.html for details.
Also, as of version 0.2.x, iisnode does support WebSockets on Windows 8 and Windows Server 2012 with IIS 8. Check out http://tomasz.janczuk.org/2012/11/how-to-use-websockets-with-nodejs-apps.html for details.

Resources