Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket server on Azure with node.js

I'm trying to create a websocket server with Node.js to run on a Windows Azure. This should be simple, but I have run into problems I haven't been able to find solutions for anywhere.

This is my serverside code:

var ws = require('websocket.io')
    , http = require('http').createServer().listen(process.env.PORT)
    , server = ws.attach(http)

server.on('connection', function (socket) {
    socket.on('message', function () { });
    socket.on('close', function () { });
});

How do I know which port I need to connect to? My client code is:

var ws = new WebSocket("ws://socketiopuge.azurewebsites.net");

ws.onopen = function () {
    alert("opened");
};

ws.onmessage = function(msg) {
    alert(msg);
};

ws.onclose = function () {
    alert("closed");
};

When I run the code I get an 501 error code, and the onclode event is fired. I believe the problem is that I need to specify a port number when I create the WebSocket. Can any of you point me in the right direction? Thanks!

like image 957
JPuge Avatar asked Jul 05 '26 04:07

JPuge


2 Answers

About your question "How do I know which port I need to connect to", you would need to create an Input Endpoint and set proper PORT for it. Once you configured it, you will be using the same port in your code to bind and use.

Here is an example about Running socket.io on Windows Azure Web and Worker roles

If you host socket.io in a Windows Azure Web Role, please disable the WebSockets transport on the server because Windows Azure Web role runs on IIS7, web sockets are not supported on IIS7 yet. With Worker Role you dont need to worry about it and you can directly use Web Sockets.

like image 139
AvkashChauhan Avatar answered Jul 07 '26 06:07

AvkashChauhan


If you're using the new Azure web sites, they do not support websockets yet. The same goes for all web sites hosted through IIS - node.js or not.

Your only way of currently supporting websockets in Azure is by using worker roles and supplying your own node.js executable.

like image 34
einaros Avatar answered Jul 07 '26 07:07

einaros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!