c-sharp
  1. c-sharp-signalr-in-c

C# SignalR

SignalR is an open-source library for ASP.NET that simplifies the process of adding real-time functionality to your applications. It allows you to create and subscribe to real-time events, and provides an API for sending messages between clients and servers. In this tutorial, we'll discuss how to use SignalR in C#.

Syntax

The syntax for using SignalR in C# starts with creating a SignalR Hub. Here's an example of how to define a SignalR Hub:

using Microsoft.AspNet.SignalR;

public class MyHub : Hub {
   public void Send(string message) {
      Clients.All.broadcastMessage(message);
   }
}

In this example, we create a hub called "MyHub" that defines a method called "Send". When this method is called, it broadcasts the message to all connected clients.

Example

Let's say we want to create a simple chat application using SignalR. In this application, users can send messages to the server, which will then broadcast the message to all other connected clients. Here's how we can implement it:

Server-side

using Microsoft.AspNet.SignalR;

public class ChatHub : Hub {
   public void Send(string name, string message) {
      Clients.All.broadcastMessage(name, message);
   } 
}

Client-side

<!DOCTYPE html>
<html>
<head>
   <title>SignalR Chat Example</title>
   <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
   <script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
   <script src="~/signalr/hubs"></script>
   <script>
      $(function () {
         var chat = $.connection.chatHub;
         chat.client.broadcastMessage = function (name, message) {
            $('#messages').append('<li><strong>' + name + '</strong>: ' + message + '</li>');
         };
         $('#sendmessage').click(function () {
            chat.server.send($('#displayname').val(), $('#message').val());
            $('#message').val('');
         });
         $.connection.hub.start();
      });
   </script>
</head>
<body>
   <div>
      <input type="text" id="displayname" placeholder="Display Name" />
      <input type="text" id="message" placeholder="Message" />
      <input type="button" id="sendmessage" value="Send" />
   </div>
   <ul id="messages"></ul>
</body>
</html>

Output

When we run the chat application above, we'll see a chat interface where we can enter our name and a message, and send the message to the server. The server then broadcasts the message to all other connected clients, including the sender.

Explanation

In the example above, we created a SignalR Hub called "ChatHub" that defines a method called "Send". This method takes two parameters: a name and a message. When this method is called, it broadcasts the message to all connected clients by invoking the "broadcastMessage" method on the "Clients" object.

On the client-side, we create a SignalR connection and define a function to handle incoming messages. We then bind this function to the "broadcastMessage" event on the hub. When the user clicks the "send" button, we call the "send" method on the hub with the user's name and message.

Use

SignalR can be used in a variety of applications, including real-time dashboards, chat applications, and collaborative editing tools. It's particularly useful for scenarios where you need to notify clients in real-time about events occurring on the server.

Important Points

  • SignalR uses WebSockets by default, but it can fall back to other techniques like long polling or Server-Sent Events if WebSockets are not supported by the client or server.
  • SignalR automatically handles reconnections, so you don't need to write any code to handle reconnection logic.
  • SignalR allows you to send both client-to-server and server-to-client messages.

Summary

In this tutorial, we discussed how to use SignalR in C#. We covered the syntax, example, output, explanation, use, and important points of SignalR in C#. With this knowledge, you can now use SignalR in your C# applications to add real-time functionality and improve the user experience.

Published on: