Using WebSockets with Windows Azure Web Sites

If you missed the announcement the other day, WebSockets are now supported in Windows Azure Web Sites. This means you can now use things like Socket.io from web sites instead of having to use a Cloud Service worker role.

To get this working

  1. Create a an application that uses WebSockets using a language that works with Web Sites. For example, I'm using the Socket.io example chat app.

    Since the chat example has some hardcoded paths for socket.io, as well as the port, the following changes have to be made for it to work with Web Sites:

    1. Edit app.js and change the following:

      original line

       , sio = require('..//..//lib//socket.io'); 
      

      updated line

       , sio = require('socket.io') 
      

      original line

       app.listen(3000, function () { 
      

      updated line

       app.listen(process.env.PORT, function () { 
      
    2. Use npm to add socket.io as a dependency in the package.json file:

      npm install socket.io --save

  2. Create a new web site and deploy the application to it. I used the cross-platform command-line interface: to create a new web site. Here's the commands I used:

     azure site create mywebsocketapp --location "East US" --git 
    

    That gives it a name, a location, and sets up git in the folder you run this command from (the folder that your application files are in.) The --git parameter also creates a remote named 'azure' that can be used to push the files up.

    1. Add and commit the files:

       git add .
      
       git commit -m "initial commit"
      
    2. Push to azure:

       git push azure master
      

      As this command processes, it should install all the required modules on the server. This can take a while the first time through.

  3. Go to the Windows Azure Management Console, select web sites, select your web site, and go to the CONFIGURE page. Here, scroll down and enable Web Sockets, then click the Save icon.

    Once you turn the switch to on and save the changes, WebSocket support should start working for the application.

Summary

That's it. That little switch in the UI is all there is to enabling it. Hopefully support for enabling it will be coming to the command-line tools, but for now it looks like you have to enable it in the portal.