83

I currently have a TCP server application written in .Net that receives and submits messages to clients. I am looking at building a web application so need the communication layer.

I have built a Node.JS + Socket.IO app which connects to my TCP server and then pushes communication to the web application and all works fine.

I have just read about SignalR as an alternative to keep it in the .Net stack.

However I have also found that I could write a C# Websocket Server, a basic demo here

I assume that this basic server is what SignalR is but obviously with a lot more functionality in it?

What I'm trying to decide is do I just append my current TCP application with a Websocket server or do I go down a separate SignalR or Node.js route? Out of interest how does a SignalR application run, is it as a Windows service, console app or IIS service?

Community
  • 1
  • 1
Jon
  • 38,814
  • 81
  • 233
  • 382
  • 1
    For info, you might try SuperWebSocket too. For now, I just wrote a WebSocket server directly. There's only 2 key protocols in play (the rest are virtually identical variants), so it isn't massive. I'm partly waiting for .net 4.5 / win8, which includes WebSocket server support OOTB. – Marc Gravell Mar 02 '12 at 17:26
  • @MarcGravell Thanks. Here is a post showing .Net 4.5 Websocket server http://marcinbudny.blogspot.com/2011/10/websockets-in-net-45-simple-game.html I'm not sure how the server runs though. – Jon Mar 02 '12 at 18:32
  • 1
    Don't forget that socket.io does a lot more than just websockets, it also does fallback to long-polling, etc. – mtsr Mar 05 '12 at 16:24
  • @mtsr I believe SignalR has fallback as well – Jon Mar 05 '12 at 20:28
  • Sure, but when writing a basic C# websocket server, you would have to implement it all yourself. – mtsr Mar 06 '12 at 14:50
  • 1
    @MarcGravell A year after. which do you prefer ( after experiencing vs2012 signalR and node) ? – Royi Namir May 12 '13 at 12:36
  • 2
    @Royi I'll answer that indirectly: we're still our custom socket server, and plan to release the code as F/OSS – Marc Gravell May 12 '13 at 12:37

4 Answers4

82

SignalR is like Socket.IO in that it supports transport negotiation/fallback. It is a framework and not a server, so you need to host it on a server of some sort. We have hosts for ASP.NET, OWIN (e.g. Kayak) and self-host, so you can run it in your own process easily, e.g. a Windows service.

SignalR has supported clients for browsers (JS), .NET, Windows Phone 7 and Silverlight. There are also contributed clients for things like iOS, Mono Touch, etc.

SignalR will give you a much higher level API than raw sockets which is its big advantage, allowing you to do things like "RPC" from server to clients in a broadcast (or targeted) fashion.

Damian Edwards
  • 7,289
  • 1
  • 25
  • 19
25

Other implications

I've used both technologies and work on both sides of the .NET / node stacks.

  1. Although I prefer the node side these days, if you only work in .NET, SignalR is the obvious choice. Conversely, if you build all your projects in node I would go with socket.io or sockjs. If your scope is narrow enough that you don't need to worry about fallbacks and that sort of thing, I'd recommend checking out ws module since its simpler and lighter on your dependencies. In the past, socket.io has been a pain on Windows due to install issues with node-gyp failing to install native dependencies (node-gyp requires many configuration steps that vary wildly depending on which version of Windows you have but is required for C++ native built modules). UPDATE This Windows bit is not so much relevant anymore thanks to windows-build-tools.
  2. If you have a load balancer and are planning on running SignalR, you are going to need to setup SQL or Redis as a backplane to bypass the load balancer. You will have similar issues to deal with on the socket.io side and there are [multiple supported methods][1] (1 of which is redis also).

Update - removed jquery info since it is no longer applicable

cchamberlain
  • 17,444
  • 7
  • 59
  • 72
  • 3
    Actually SignalR JavaScript client library 2.2.0 (released back in January 2015) supports any version of jQuery >= 1.6.4. i.e. you will not have to duplicate versions of jQuery (which would break anyway). I have just verified this is the case with a sample chat application running in Chrome, IE and Firefox with SignalR JavaScript client version 2.2.0 and jQuery 2.1.4. Also it is highly likely that most browsers have a compatible copy of jQuery cached. – dmcquiggin Jun 11 '15 at 22:42
  • 1
    For those who are not aware, SignalR now comes as a service provided by Azure who manage the scalability aspect of it for you. They also have a free tier to play around with. – Shayan C Apr 25 '19 at 08:49
15

Developing a scalable / thread safe TCP server may not be an easy task. On the other hand, there are very nice resources on the internet in order to start your own. For example, if you are just looking for some nice open source WebSocket projects, my advice would be ;

Alchemy Project : Open Source C# WebSocket Library

Fleck Project : Open Source C# WebSocket Library

SignalR could be nice but it needs Windows Server 8 / IIS 8 in order to provide WebSocket feature.

On the commercial product side, especially considering the websocket feature is not available on all the browsers, i recommend PokeIn WebSocket and reverse Ajax Library. Starting from version 2.0 it has built-in WebSocket server. Details available from here

Zuuum
  • 1,495
  • 11
  • 18
0

It is a really cool way to handle real time messages or data that must be transferred to other users in real time. I did use it with react.js before and it was very useful.

SignalR works for both Polling and Websockets but it will not be a good idea to use Polling as it uses a lot of bandwidth and could slow your application.

On websockets you will have an always opened connection between users and the server that is first established when the user enters on a specific part of the app. Please always check if the connection is still active and if for some reason the connection timed out, reinitialize the connection (you can do that by using setInterval in js every 5 seconds or whatever). Then be careful on how messages are spread to not send to the wrong people and take care of accessibility (use for example a guid as group id for a specific connection). And lastly if data transferred is critical please use encryption / decryption methods.

Also a big advantage with SignalR is that it does not use cache on server side.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 01 '23 at 18:07