Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Hybrid Application Using ASP.NET MVC3 and Node.js

DZone's Guide to

A Hybrid Application Using ASP.NET MVC3 and Node.js

Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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

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

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

image image
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.  

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Tamizhvendan Sembiyan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}