1
Reply

How can we send a message using SignalR in .net core

Jayraj Chhaya

Jayraj Chhaya

1y
1.1k
0
Reply

I have an advertisement display project managed by SignalR in .NET Core. It is currently working well, and the client is happy with it.

However, I now want to implement subscription-based management for displaying data. If the subscription has expired, the advertisements should not be displayed; otherwise, they should display as usual. The client connects to the SignalR hub only once, after which static data is displayed on the screen, and the client does not call any methods from the SignalR hub.

How can I manage the subscription scenario in this situation? Since the client is not calling any methods from the SignalR hub and only connects once, how can I fit the subscription scenario into this setup?

    In ASP.NET Core, SignalR is used to enable real-time communication between the server and connected clients (such as web browsers). To send a message using SignalR, you first create a Hub class that acts as a communication endpoint. Inside the Hub, you can use the Clients object to send messages to all connected clients, specific clients, or groups. For example, by creating a method like Send message in the Hub, the server can broadcast the message using Clients.All.SendAsync("Receive message", user, message), which sends the message to every connected client in real time. After creating the Hub, it must be registered in the application configuration (Program.cs) using app.MapHub<Chat hub>("/Chat hub");. Clients such as JavaScript applications then connect to this hub URL and listen for the Receive message event to receive messages instantly. This approach allows applications like chat systems, live notifications, dashboards, and collaborative tools to update data immediately without refreshing the page.