A Hybrid Application Using ASP.NET MVC3 and Node.js
If you ask me what I have learnt in my profession so far, I would say the above line as the first one. One technology / platform / language is good at solving one kind of problem and bad at solving other kinds 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 came across an excellent presentation from Neil Ford “10 ways to improve your code”. As an 8th way he talked about “Polyglot Programming”. Its about picking the right tool to do the right job. I was 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 are not meant for creating real-time applications and they address a 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
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
The ASP.NET MVC3 side
Chat feature in Action
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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)