Showing posts with label SignalR. Show all posts
Showing posts with label SignalR. Show all posts

Friday, 18 May 2012

SIGNAL-R Implementation

What is Signal-R?
 
Signal R is an asynchronous library for .NET to help build real-time, multi-user interactive web applications.

Pushing data from server to client would always been tough process.By using Signal-R you find it damn easy. To design application which require persistent connection between server and client like Chat application, Stock ticker application, Signal R would be very useful for the implementation of this functionality.

Where you can find Signal-R?
Signal R is included under the NuGet package. Go to visual studio -> NuGet Package Manager and run below command in your application:

Install-Package SignalR
 

Steps to make Signal R working:
 
1.Install Signal R Package
Install Signal R package from NuGet by executing above command in package explorer.

2.Create ServerHub Class
After installing the Signal R package, it will include all necessary DLL and script files under your application. After that create a separate folder of Signal R and add a class file. This class file will be a hub of all methods which are going to use for client communication. Inherit Hub class of signalR under this class. Further define methods through which server can push data to clients.
[HubName("messageHub")]
 public class MessageHub:Hub
 {
  ///


  /// This method is called through SignalR (JS) of post message section.
  ///

  /// message
">message id: message text which needs to sent to all clients.
  public void sendMessage(string message) // Server Method which call by client to trigger push
  {
   Clients.showMessage(
message); // Client method which call by server on push
  }
 

3. Define connection within client
Once define the server hub class, you need to add server connection code under client using JS. Include below script for connection under client file:


4. Call Hub method from client
Now, its require to trigger the server hub method from client.Client has already started the connection and we have the object of hub in client file. Now we just need to execute the server hum method in the JS method of any client action like button click etc...

blog.sendMessage("this is broadcast message"); 

5. execution of client method from hub method
In step 4, Client call the server hub method by passing the message text. Visualize that in a page there is a facility to broadcast message where client add the text into text box and click on send method  which execute the method as shown in step-4. 

Now under hub method, sendmessage contains the code 
Clients.showMessage(message);  
Hub class now execute find the showMessage method under the connected clients and triggered that method.


Find below a complete visualization for the Singal-R processing:



References:
1.https://github.com/SignalR/SignalR