How to Send Data From Client to Server in SignalR

SignalR is a real-time communication library for web applications. It allows server-side code to push content to clients in real time, without the need for clients to periodically request updates. I will provide step by step guide to send data from client to server in SignalR.

This makes it ideal for building real-time chat systems, collaboration tools, gaming, and other applications that require low-latency communication between the server and clients.

One unique feature of SignalR is its ability to automatically negotiate the most efficient communication protocol between the server and clients, whether it be WebSockets, Server-Sent Events, or Long Polling, to ensure the best performance possible.

Here We Discussed How to Send data from the Client To Server in SignalR in C#.

Here is a step-by-step guide for sending data from client to server using SignalR:

  • Add SignalR to your project: You can add SignalR to your project by using NuGet Package Manager in Visual Studio or by running the following command in the Package Manager Console:
    Install-Package Microsoft.AspNetCore.SignalR (Client Side)
  • Add this code to the Client Side where you want to call the server.
var connection = new HubConnectionBuilder().WithUrl("Your LocallHost Url Here /notificationHub(Your Server Class Name)", options =>
{
    options.Transports = HttpTransportType.WebSockets;
}).WithAutomaticReconnect().Build();
await connection.StartAsync();
SignalR Client To Server Connection

After This Write the following Code after Above Code.

connection.On<object>("ReceiveMessage", async (message) =>
{

//ReceiveMessage is in the Server Side
    await connection.InvokeAsync("NotificationMessage", "ClientSide Id", message);
//NotificationMessage is the method in notificationHub (Class) in Server Side and the ClientSide Id,message are Two Parameters of this method.
});

connection.On<object>("Client", async (message) =>
{
    Console.WriteLine(message);
    
});
Connection To Server In Client Side

On Server Side.

  • Add Class in Server Side Named as NotificationHub
  • Inherit it from Hub.
public class NotificationHub : Hub
    {
        public void NotificationMessage(string message, string userId)
        {
            Clients.Client(userId).SendAsync("Client","Connected Successfully");
        }


        public override Task OnConnectedAsync()
        {
            ConnectedUser.UserId.Add(Context.ConnectionId);
            Clients.Client(Context.ConnectionId).SendAsync("ReceiveMessage", Context.ConnectionId);
            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            ConnectedUser.UserId.Remove(Context.ConnectionId);
            return base.OnDisconnectedAsync(exception);
        }

    }
Server-Side Notification Hub Class

Client to Server in SignalR in C# Video:


6 thoughts on “How to Send Data From Client to Server in SignalR”

Leave a Comment