15
Feb
13

Azure websocket server with node.js and socket.io

Peter Daukintis

If you haven’t previously done this download your Azure publish settings file from here https://windows.azure.com/download/publishprofile.aspx

This will contain your management certificate and details for each of your Azure subscriptions.

 

If you have multiple Azure subscriptions ensure that you have set the ‘current’ one to be the one you wish to use:

Select-AzureSubscription –SubscriptionName your_subscription_name

You can confirm this is set by using,

Get-AzureSubscription -Current 

Then, to create the Azure project

New-AzureServiceProject your_project_name

This will create the files needed by the Azure cloud project (including deployment settings, cloud/local configuration files and a service definition file).

Add-AzureNodeWorkerRole

This will add a WorkerRole folder which contains the Node project

ps2

then, from within the WorkRole folder,

npm install azure (optional – enables access to azure services within your node project see https://npmjs.org/package/azure)

npm install socket.io

navigate to the worker role folder and replace the code in server.js with the following:

   1:  var http = require('http'); 
   2:  var io = require('socket.io'); 
   3:  var port = process.env.port || 1337; 
   4:  var server = http.createServer(function (req, res) { 
   5:      res.writeHead(200, { 'Content-Type': 'text/plain' }); 
   6:      res.end('Hello World 2\n'); 
   7:  }).listen(port);
   8:   
   9:  var socket = io.listen(server); 
  10:  socket.on('connection', function (client) { 
  11:      socket.emit('news', { hello: 'world' }); 
  12:      client.on('message', function(){ 
  13:          console.log('message arrive'); 
  14:          client.send('some message'); 
  15:      });
  16:   
  17:      client.on('disconnect', function(){ 
  18:          console.log('connection closed'); 
  19:      }); 
  20:  });
  21:   

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }


then

Publish-AzureServiceProject

Note that subsequent publish commands will result in an upgrade…

publish

 

Create a .NET client to test the server

Create a new c# console app in visual studio, add SocketIO .NET client Nuget package (Install-Package SocketIO4Net.Client)

SocketIONetClientNuget

Paste the following into the console app…

   1:          static void Main(string[] args)
   2:          {
   3:              Console.WriteLine("Starting TestSocketIOClient Example...");
   4:   
   5:              using (var socket = new Client("http://your_project_name.cloudapp.net/"))
   6:              {
   7:                  socket.Opened += SocketOpened;
   8:                  socket.Message += SocketMessage;
   9:                  socket.SocketConnectionClosed += SocketConnectionClosed;
  10:                  socket.Error += SocketError;
  11:   
  12:                  socket.Connect();
  13:   
  14:                  Console.ReadLine();
  15:              }
  16:          }
  17:   
  18:          private static void SocketConnectionClosed(object sender, EventArgs e)
  19:          {
  20:              Console.WriteLine("CLOSED");
  21:          }
  22:   
  23:          private static void SocketError(object sender, ErrorEventArgs e)
  24:          {
  25:              Console.WriteLine(e.Message);
  26:          }
  27:   
  28:          private static void SocketMessage(object sender, MessageEventArgs e)
  29:          {
  30:              Console.WriteLine(e.Message.MessageType.ToString());
  31:          }
  32:   
  33:          private static void SocketOpened(object sender, EventArgs e)
  34:          {
  35:              Console.WriteLine("OPENED");
  36:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

That’s it.

About these ads

0 Responses to “Azure websocket server with node.js and socket.io”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: