nodejs socket.io with IIS Node - iis

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.

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.

client-server websocket

I try to make a client-server app with socket.io.
Socket.io server seems work but the client cannot connect to it. The error i get is about the '/socket.io/socket.io.js' like what it is unable to load it.
So here are my questions
is it mandatory to have server and client in the same folder as we can see in the official demo ?
can we make a nodejs socket.io server without express ?
Depending on how your project is setup, you need to create 2 server files, 1 for the app, and one for the websockets, and every time a user opens the app it should open(and be told where to try and open the connection) a connection to the websockets server. On my websockets apps I have the app running on localhost:3000, and websockets server on localhost:3001 (and tell the app to look for a server on 3001), so really you don't need to have the server files in the same folder, they can be in 2 opposite ends of your computer, as long as the app points to the server, then your fine, once a connection has been opened, the websocket server will see the client, and it should work! Let me know if that make sense.
No, you can download socket.io front-end lib from another sources, for example cdn. Be sure you installed the right version.
Yes, you can make it without express. Express is just another option for creating an socket.io server.
For example, currently in my project that is written in another back-end node framework i'm using the code below to establish the socket.io server.
const io = require("socket.io")(2337);
io.on("connection", socket =>
// some code
)
I fixed the problem of websocket communication between my server and my client.
After inspected my html client file, i saw on the console this error message 'ReferenceError: io is not defined'.
I google that error and i found this.

nodejs + socket.io + azure. continuous polling issue

I am facing some problem with socket getting keep polled continuously without gap. Not sure whats happening.
If same code placed in digital ocean cloud, it's not happening.
Little background:
I'm running azure VM behind load balancers.
If you created load balancer using Application Request Routing with IIS, you'd need to Disable WebSocket when using socket.io on node.js.
If you are using the WebSocket support in socket.io on node.js in your
site, you will need to disable the default IIS WebSockets module by
adding the below snippet to your web.config or applicationHost.config.
If this is not done, the IIS WebSockets module will attempt to handle
the WebSocket communication rather than letting this task fall through
to node.js (and hence your application). This will result in
unexpected errors when you attempt to access your site.
<system.webServer>
...
<webSocket enabled="false"/>
</system.webServer>
Updating to latest socket library in nodeJS fixed this issue. Try your luck!

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

socket.io websocket connection invalid (latest chrome)

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.

Resources