DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Running Socket.io on Windows Azure Web and Worker Roles

Running Socket.io on Windows Azure Web and Worker Roles

Mariano Vazquez user avatar by
Mariano Vazquez
·
Jan. 23, 12 · Interview
Like (0)
Save
Tweet
Share
6.51K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we are going to review how to configure a Node.js + socket.io server both inside a web and a worker roles (if you are not familiar with what a web and worker role is, read the following MSDN article).

Both approaches work with nearly the same code (with a minor tweak to disable WebSockets in Web roles), which leads the decision on what to choose depending solely on your own requirements and needs.

You start from the basic 'Hello World' template created by the Windows Azure Powershell for Node.js cmdlets to develop both roles, with the proper modifications to send messages via socket.io. We have created a client application you can use to test that everything is in place; it simply opens a connection to the server (either a Web or a Worker role) and shows the messages it receives.

Connecting to a socket.io server

Below is the javascript code for a very simple client. Notice that it is cleaning the label that stores the message every time you click the button. This way you can easily tell how much it takes the whole emit-receive flow to complete. Remember to specify the server URL and port.

<script type="text/javascript">
    var socket;
    $(document).ready(function () {
        $("#startButton").click(function () {
            $("#returnMessageLabel").empty();
            if (!socket) {
                socket = io.connect("http://<YOUR-SERVER-URL>:<YOUR-PORT>/");
                socket.on('helloBack', function (data) {
                    $("#returnMessageLabel").text(data.message);
                });
            }
            socket.emit('sendMessage', { message: 'Hello there!' });
        });
    });  
</script>

You should get back something like this:

Now, let's look at the server code.

Running on a Windows Azure Worker role

The Worker role approach is fairly straightforward. You just need to install the socket.io module on the role and replace the code in the server.js file with the following:

server.js

var port = process.env.port || 81;

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)

app.listen(port);
console.log('socket.io server started on port: ' + port + '\n');

function handler (req, res) {
  res.writeHead(200);
  res.end('socket.io server started on port: ' + port + '\n');
}

io.sockets.on('connection', function (socket) {
  console.log('user connected');

  socket.on('sendMessage', function(data){
    console.log('user sent the message: ' + data.message + '\n');
    socket.emit('helloBack', { message: 'Hello back!' });
  });
});

There is nothing specific to Windows Azure as you can see. We deployed this to an extra small instance on Windows Azure and tested it with Internet Explorer 9 and Google Chrome.

Since Internet Explorer 9 does not have support for WebSockets (IE10 will), it will degrade the connection to xhr-polling. By default, socket.io is configured to use the following transports (in this order): websocket, htmlfile, xhr-polling and jsonp-polling

Running on a Windows Azure Web role

If you host socket.io in a web role, you will have to disable the WebSockets transport on the server. This is because Web roles run in a pre-configured IIS7, and IIS doesn't support web sockets yet. Override the transport mechanism to use, for instance, xhr-polling with a 10 sec. polling duration. No matter which client tries to connect to the server, that will be the transport method used.

To configure the transport, add the dollowing snippet to the server.js file with the same code you used for in the worker role approach, but add the following lines at the end of the file:

server.js

...
io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
});

If you don't add this fix, you will experience some initial delay in browsers that support WebSockets (like Chrome). That delay is generated because it will try to use WebSockets as first option.

Alternatively, you could configure an array of allowed methods (instead of one), using the following code:

server.js

io.configure(function () { 
  io.set('transports', [
    'xhr-polling'
  , 'jsonp-polling'
  , 'htmlfile'
  ]);
  io.set("polling duration", 10); 
});

Conclusion

To sum up what we have learnt in this article:

  • You can host socket.io in Windows Azure
  • It works in both worker and web roles.
  • If the browser supports WebSockets it will try that first.
  • If you use a Web role, remember to disable WebSockets transport, as IIS currently doesn't support it.

 

Source: http://nodeblog.cloudapp.net/running-socket-io-on-windows-azure-web-and-worker-roles

Socket.IO azure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • NoSQL vs SQL: What, Where, and How
  • Solving the Kubernetes Security Puzzle
  • Stop Using Spring Profiles Per Environment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: