ASP.NET SignalR is an async signaling library for .NET to help build real-time, multi-user interactive web applications. In this article we will see, how do create a basic application using ASP.NET SignalR.
Previously SignalR was a standalone entity, now it’s integrated as part of ASP.NET framework. You can check the following fall update.
We can check the following documentation to understand the basics about SignalR.
We will create a simple application that the user can drag and drop the Div/Box and the same position of the box will be updated to all clients. First we need to add a reference for the SignalR dlls; we can use the Nuget package also to add the references.
We will create a simple “Hub” to broadcast the required actions (method) from the server as explained below. Please note that we need to add the “Microsoft.AspNet.SignalR.Hubs” namespace and the application namespace should be unique across the application files.
In the preceding script we have three parts:
- We need to create a connection to the server and the same “Hub” name should be used in this connection statement.
- We can define the client side event called “shapemoved” and do the actions at the client side. Here we set the CSS property to the “shape” div. If we notice the “Hub” code we are calling this client side event from the server.
- The final one to do is we need to call a server side method “action” which is a “Hub” method. We called the “action” method in the client side “drag” function.
That’s all, now we will see the result in action, if we drag the box in one browser and it’s automatically updated in other clients.
ASP.NET SignalR is a new library for ASP.NET developers that make it incredibly simple to add real-time web functionality to your applications. What is “real-time web” functionality? It’s the ability to have your server-side code push content to the connected clients as it happens, in real-time.