Issue with socket.io and domain name - node.js

I get an issue using socket.io.
My code : var socket = io.connect('SUBDOMAIN.DOMAIN.fr');
Response error :
WebSocket connection to
ws://SUBDOMAIN.DOMAIN.fr/socket.io/?EIO=3&transport=websocket&sid=u2V-6uOMZrBtnCK5AAAH
failed: Error during WebSocket handshake: Unexpected response code:
503
My SUBDOMAIN.DOMAIN.fr is a type A domain.

What this actually means is that the environment hosting your socket.io service is not WebSocket enabled.
When socket.io first establishes a connection it will use long-polling, once connected, it then attempts to upgrade the connection to using WebSocket. If your hosting environment doesn't support WebSocket then you will see this message. It doesn't prevent socket.io from working it simply means it attempted to upgrade to WebSocket as the transport but failed. Now, if you explicitly state that socket.io should only use WebSocket, then your connection will not work.
An example would be IIS 7.5 or below. WebSocket isn't supported in IIS until version 8.0, so anyone using socket.io in an IIS 7.5 or below environment would see this message every time a connection is established.
This is discussed in the Engine.io Goals & Engine.io Architecture.
Engine.io is what socket.io is built on top of.

Related

how to resolve socket-io send connect request infinitely?

I'm new in socket-io. socket-io working fine but keep sending connect request infinitely to server.
Here is my client ts :
private url = environment.socketServer;
constructor() { this.socket = io(this.url) }
The usual reason for a socket.io client to try to connect over and over is if the socket.io version on the server and client are not compatible. The client connects, the server finds the version is incompatible and drops the connection and the client then tries to connect again, over and over.
Other possible issues:
Server infrastructure (such as load balancers, proxies, firewalls, etc...) are not properly configured to allow webSocket connections.
You're trying to connect to a cluster, but it isn't configured for sticky connections that will "bind" a socket.io client to the same server.
You're confused about how a socket.io connection starts. It is normal for the client to start with a couple web connections in a row (polling) until it realizes that both sides support a webSocket and then socket.io switches over to webSocket.

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.

websocket server using nodejs+socket.io cannot be connected by tester sites while ws can

I want to create a websocket server. I heard that socket.io is a good choice.
I tried socket.io with nodejs(v4.4.7) (npm install --save socket.io), using its sample server side code. A little confused why the client side code is using "http://" rather than "ws://" protocol, but after I setup a real server for testing, I found both "http//" and "ws//" will work using the official code.
Everything is fine till now. But soon I found I can't establish a connection using third-party online tester sites like:
1. www.websocket.org/echo.html
2. www.blue-zero.com/WebSocket
The connection seemed never established or closed asap connected,
I found "Firefox can't establish a connection to the server at ws://mytestserver:8888/?encoding=text" in Firefox console,
or "WebSocket connection to 'ws://mytestserver:8888' failed: Connection closed before receiving a handshake response" in Chrome console.
At last I changed socket.io to ws (npm install --save ws). using sample code from github.com/websockets/ws. all tester sites worked well.
(Of course, my final purpose is not to make a tester site work. the fact is the websocket lib based on nopoll integrated in my chip has exactly the same behavior as the tester sites.)
Anybody knows the reason why socket.io does not work with 3rd-party clients while ws does? Thanks a lot.
socket.io requires a socket.io server on the server-side end of things. It will not connect to only a webSocket server.
While socket.io uses webSocket as the underlying transport, it adds a layer on top of webSocket to implement a whole bunch of additional features and that requires server-side support for socket.io. So, you can't connect to a plain webSocket server with the socket.io client.
You must match:
webSocket client <==> webSocket server
socket.io client <==> socket.io server

Node.js, socket.io and IIS 7

I was wondering if it would be possible to host a node.js socket.io app in IIS 7 using iisnode? I've read that WebSockets are only supported in IIS 8 but I also read that it might be possible so I am a little bit confused.
Thanks!
From what I've read IIS 8 is the first version of IIS with WebSockets, until then longpolling is used.
As for the output in your comment, you will see that because the Socket.io client is going to attempt to establish a connection by default with longpolling and then will attempt to upgrade the connection to WebSockets, unless you explicitly specify that it shouldn't. This can be done in the options passed to the server and the client at instantiation via the transports Array.
An explanation of how Socket.io works is available here
See the documentation for the Socket.io Server Engine here and the Socket.io Client Engine here

Why HTML WebSocket access Socket.io web socket server failed?

As socket.io said, it supports WebSocket, so, I use HTML5 standard web socket api to access socket.io server, but I always get below error:
WebSocket connection to 'ws://localhost:8080/' failed: Connection
closed before receiving a handshake response
Then, I tried to use socket.io client in js to access socket.io server, it works, and by chrome network monitoring, i found it using web socket protocol correctly.
Did anyone ever try W3C Websocket api to access socket.io server and met similar issue? or any ideas or clues for my problem? appreciated!
Test code is here: https://github.com/piginzoo/socketiotest
Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either.
And I find this in socketio github issues:
Socket.IO is not a WebSocket server. Please see ws instead.
https://github.com/socketio/socket.io/issues/3022
The error message you got was because you used the wrong url. The error message "Connection closed before receiving a handshake response" actually told you this (not receiving a response)
It should be 'ws://localhost:8080/socket.io/?EIO=3&transport=websocket'
Check here https://socket.io/docs/client-api/#With-custom-path & https://socket.io/docs/internals/ for details
e.g.
"EIO=3" # the current version of the Engine.IO protocol
BUT as other answer said and actually that was what socket.io readme said
Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server.
I have tested that to verify both ws & browser native Websocket can't not connect to a Socket.IO server. They are both immediately disconnected from Socket.IO server after they connects, with Socket.IO server send out the error message socket id xxx has disconnected with reason parse error
The ws author also mentioned here https://github.com/websockets/ws/issues/1390
using plain WebSocket to talk with a Socket.IO server does not work seamlessly.
If you want to use browser native Websocket you can use ws can server.
We had exactly the same problem. Ended up with ws websockets. Not sure may be there is better solution.

Resources