I've migrated my blog

Thanks for visiting my blog. I am no longer maintaining this and I've migrated the blog to my personal domain . If you would like to follow my blog kindly use my new RSS feed

Friday, June 1, 2012

A hybrid application using ASP.NET MVC3 and Node.js

Each and every technology has its pros and cons and there is no silver bullet!

If you would ask me what you have learnt in your profession so far, I would say the above line as the first one. One technology / platform / language is good at solving one kind of problems and bad at solving other kind of problems. The key is leveraging existing platforms with languages targeted at specific problems and applications to solve the business problems in hand.

Three months ago I have come across an excellent presentation from Neil Ford “10 ways to improve your code”. As a 8th way he talks about “Polyglot Programming”. Its about picking the right tool to do the right job. I am inspired by this presentation and started exploring the “right tools”.

In the pursuit of “Right tools”, I’ve come across the young awesome framework node.js which is really cool and perfect for real-time applications. On the other hand frameworks like ASP.NET MVC3 is not meant for creating real-time applications and they address different set of other problems.

In this blog post, I going to share my recent exploration on leveraging node.js in an ASP.NET MVC3 application using a “Chat” feature. Though we can achieve it using SignalR, I’ve preferred node.js as it is a right tool for the chat feature IMO also I wanted to get my hands dirty in creating a hybrid application.


The Node.js Part

image

The chat server that we are going to use in the MVC3 application has been created using socket.io which makes web-socket programming insanely easy!
Its listen for three events
  • join – It will be triggered when a new user joined the chat and it broadcast the user name to all the connected users
  • message – It willed be triggered when an user send a message in the chat application and broadcast it to the other users
  • disconnect – It willed be triggered when an user closes the chat or closes the browser
That’s it bang!


The ASP.NET MVC3 side

image

The controller side of Chat just renders the “Index view”. The index view has the following javascript code, and it completes the client side part of the chat application

image


Chat feature in Action

imageimage
image
image
image
image


Summary

The objective behind this blog post is just to share the knowledge that I’ve gained in my recent exploration and its just a proof of concept of how to leverage node.js in an MVC3 application. The bottom-line is “Identify and pick the right tool to do the right job”. If you want to play with the source code, don’t forget to clone the repository in my git-hub page.  

8 comments:

  1. Replies
    1. Sure! By the way this is just my exploration on creating a hybrid app by combining two different technologies.

      Delete
  2. I don't see how you used MVC in this, you're just rendering a view, which you can do in normal HTML...

    ReplyDelete
    Replies
    1. Good catch Rami. To keep the blogpost simple, I didn't focus much on MVC side of the app and it could be any app. The intention of this blog post is how to make use of node.js from an MVC application.

      Delete
  3. The exploration on hybrid application using ASP.NET MVC3 and Node.js in two different Technology using with normal HTML is very useful..

    ASP.Net Development India

    ReplyDelete
  4. i dont know how to execute this application ...please can u explain step by step procedure....

    ReplyDelete
  5. Node.js framework framework allow web developers in writing the server side scripting code in JS and it seems like Microsoft has helped the developers in number of ways as they have kept all the essentials in one place so that developers can get everything at one place. The team member at Microsoft Web Platform team support states the thought behind the invention as "Not re-ding the things that has been done at once and doing well"

    ReplyDelete