Original address:
What's SignalR and why should I use it?
When I first heard about SignalR, I am wasn't sure what is the point of it. The official description says that SignalR was a library that can being used with any ASP. Application to add real-time COM Munication. But since-they meant by real-time is not totally clear to me, and the only example is a chat, I pushed it asid E for a while.
When I started I side project, I also started thinking about SignalR again. One of the things I want to do is refresh the schedule as soon as a change is made:if a user makes a modification, it s Hould is visible to all other users. The classic-like-something-would be-to-call the server at regular intervals to get the status of the schedu Le, but to has pseudo real-time update, you must call the server pretty often.
With SignalR, the server can call a JavaScript methods on all the clients by itself when updates is required. The library would handle the connection needed to achieve This:by default WebSocket was used, but it would fallback automaticall Y to older connections types if are not available in the WebSocket browser. The JavaScript can also call the server:this can already be do with AJAX, but if two-way communication was needed it may Be easier and cleaner to does it all with SignalR.
So, using a real-time library is the the-the-go if you want-to-build an application This requires collaboration between use Rs. Common uses cases includes editors, social networks, chats or a schedule like my project.
Getting started with SignalR are pretty simple. Here's an example with my side project:sessions for an event can being modified by any user, and when a session is modified The change was visible for all the user currently logged in the application.
On the Server
First, you must add the Microsoft ASP. SignalR package to your ASP. Application using NuGet, along with the The JQuery package if you don ' t already has it.
After this, you must create a class that derives from the Hub class. All the methods of the hub can is called from JavaScript. Also, those methods can call JavaScript methods on the client.
Hide Copy Code
namespace Eventscheduling{public class Sessionshub:hub {public void sessionmodified (scheduledsession Scheduledsession) { //Call the JavaScript method sendsessionchanged on all the clients Clients.All.sendSessionChanged (scheduledsession);}}}
To enable communications between the hub and the JavaScript, you must also enable in the the SignalR Configuration Startup CLA SS of your application.
Hide Copy Code
namespace Eventscheduling{public class Startup {public void Configuration (Iappbuilder app) { App. MAPSIGNALR ();}}}
On the Client
On the client, you must include the jquery.signalr-2.2.0.js file containing the JavaScript for the SignalR library and the package ~/signalr/hubs. This are automatically generated from your classes and contains all the JavaScript required to call the Hub server and receive message from the server.
Hide Copy Code
<script src= "/scripts/jquery-1.10.2.js" ></script>
After this, when the page is loaded and ready, you must declare the JavaScript function called by the Server-side code. Since Hub The class is called SessionsHub SessionModified hub sendSessionChanged and the method of the class calls the method, this function must be de Clared in the $.connection.sessionsHub.client.sendSessionChanged variable.
Once This function is a created, you must also connect to the hubs using the $.connection.hub.start method. You can also add initialization code in the done() callback which would be called when the connection is started.
Hide Copy Code
$ (document). Ready (function () {//Create a function, the hub can call to broadcast messages. $.connection.sessionshub.client.sendsessionchanged = function (session) {//Display the session again using the new Data received from the server}); }; Start the connection and create a function that the hub would call when loaded $.connection.hub.start (). Done ($ ("#Sessi ONS "). On (" click ",". Session ", function (event) {var = $ (this); var currentsession = That.data ("session"); Currentsession.title = Currentsession.title + "Clicked"; Call the server, which'll notify all clients of the "the" in the title $.connection.sessionshub.server.sessionm Odified (currentsession); }));});
In the example, when the hubs was done starting, a click handler was added to each session on the page. When the users clicks a session, the word was Clicked added to the title of the session, and all browsers showing the page AR E notified. Since SessionModified SessionsHub The method is declared in the class of the server, it can is called from JavaScript using the $.connection.sessionsHub.server.sessionModified met Hod.
With-only this small bit of code, your now has two-way communications between the client and the server, which you can use To update the data in real-time for your users. I'll use the SignalR in the future projects, that's for sure!
What's SignalR and why should I use It?