Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating between Node.Js and ASP.NET MVC Application

I have an existing complex website built using ASP.NET MVC, including a database backend, data layer, as well as the Web UI layer. Rebuilding this website in another language is not a feasible option.

There are some UI elements on some views (client side) which would benefit from live interactivity, involving both push and pull, so rather than implement some kind of custom long polling or websocket server in asp.net, I am looking to leverage node.js for Windows, and Socket.io.

My problem is that I need two way communication between both applications. Each user should only be able to receive data once they are authorised on the ASP.NET website, so I first need communication for this. Secondly, once certain events occur on the ASP.NET website I want to immediately push this data to the Node server, to be broadcast to specific users or groups of users. Thirdly, I would like any data sent to the node.js server to be pushed to the ASP.NET website for processing, as this is where all our business logic lies. The sole reason for adding Node.js is to have the possibility to push data directly to the client, I do not want to build any business logic into it (or as little as possible).

I would like to know what the fastest method of two-way push communication is between Node.Js and ASP.NET. The only good option I'm aware of so far is to create a special listener on a specific port on the node.js server and connect to that, but I was wondering if there's a more elegant or more efficient method? I also know that you could use a database inbetween but surely this would need to be polled and would be less efficient? Both servers will be running on the same server under a Visual Studio project.

Many thanks for any help you can provide.

like image 297
Oriental Avatar asked Nov 11 '11 07:11

Oriental


People also ask

Can I use node js with ASP NET MVC?

js with ASP.NET MVC. yes, That's work great. This link also helpful for the routing and url rewrite in iisnode.

Can we use JavaScript in ASP NET MVC?

JavaScript can be used in asp.net mvc.

Is ASP.NET faster than Nodejs?

However, when it comes to high CPU-intensive works, ASP.NET performs better than Node. js. The performance also depends on the platform.

Can I use node js with ASP NET core?

This NuGet package provides a fast and robust way to invoke Node. js code from a . NET application (typically ASP.NET Core web apps). You can use this whenever you want to use Node/NPM-supplied functionality at runtime in ASP.NET.


2 Answers

I'm not an ASP.NET expert, but I think there are multiple ways you can achieve this:

1) As you said, you could make Node listen on a specific port for data and then react based on the data received (TCP)
2) You can make POST requests to Node.js (HTTP) and also send an auth-key in the process to be extra-secure. Like on 1) Node would react to the data you send.
3) Use something like Redis for pub-sub, send messages from ASP.NET (pub) and get them on the Node.js part (sub). This is even better if you want to scale your app across multiple machines etc.

like image 141
alessioalex Avatar answered Oct 03 '22 21:10

alessioalex


The only good option I'm aware of so far is to create a special listener on a specific port on the node.js server and connect to that, but I was wondering if there's a more elegant or more efficient method?

You can try to look at redis pub/sub model where ASP.NET MVC application and node.js would communicate through separate channels in order to achieve full-duplex communication. Or you can also try to use CouchDB change nofitications.

I also know that you could use a database inbetween but surely this would need to be polled and would be less efficient?

Former techniques do not require you to poll for changes, but instead they will notify you when the changes happens or channel message arrives.

like image 39
yojimbo87 Avatar answered Oct 03 '22 22:10

yojimbo87