...

/

Broadcasting Messages to All Clients

Broadcasting Messages to All Clients

Learn about broadcasting messages to all clients (.Net client, JavaScript client).

Overview

We've already broadcasted SignalR messages to all the connected clients. This was achieved by making the call on the Clients.All property in the server-side hub. But this way of broadcasting a message has its limitations. What if we want to exclude the client that has sent the message from the list of its recipients?

In SignalR, this can be achieved by using Clients.Others instead of Clients.All. And this is what we will now implement.

Open the LearningHub.cs file in our SignalRServer project and add the following method to the class:

Press + to interact
public async Task SendToOthers(string message)
{
await Clients.Others.ReceiveMessage(message);
}

This method is identical to BroadcastMessage we had previously seen, except for one little detail. Now, we need to get our clients to call it.

Applying changes to JavaScript client

...