locked
Send message to multiple users in one go RRS feed

  • Question

  • User517361227 posted

    Hi,

    I wanna implement SignalR in one of my projects but I have a question on how to send/broadcast message to multiple users at once without loop over my connections ids and send message individually to them.

    I do not want to broadcast the message to all connected users to hub which could be thousands of users and are apparently irrelevant to me to send message to unknown clients.
    I do not even want to send message to groups as this will send message to only those users who are in that group.
    So basically want to send message to all my contacts only and not rest of the world.
    Think like updating only my contacts about my online/offline status for example.

    One work around could be find my contacts' connection id from DB and I loop through it and send message one by one which does not seem quite better way of doing the thing, so please tell me if there is way to send message to only some people but not all in a one go.

    Thanks
    Vinod

    Sunday, June 18, 2017 7:08 PM

Answers

  • User1967761114 posted

    Hi vinod_praviram,

    According to your description, I’m unsure which way you would like to use.
    if you want to send a message to a specify user by SignalR, you could know the connectionId of the user, such as the following code:

    Clients.Client("connectionId").sendMessage("hello word");

    As the same principle, send message to multi users: 

    Clients.Clients(new List<string>() { "connectionId1", "connectionId2",... }).sendMessage("hello word");

    Send message to multi users by group(you could add the connectionId to group before invoke):

    Clients.Group("groupName").sendMessage("hello world");

    I suggest that you could read the tutorial before you using SignalR by the following link.

    https://www.asp.net/signalr

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 5:26 AM

All replies

  • User-1509636757 posted

    how to send/broadcast message to multiple users at once without loop over my connections ids and send message individually to them.

    You may think on creating a group of users to whom you need to send message and then send message to that group: c# - SignalR .Net client: How do I send a message to a Group?

    Monday, June 19, 2017 3:22 AM
  • User1967761114 posted

    Hi vinod_praviram,

    According to your description, I’m unsure which way you would like to use.
    if you want to send a message to a specify user by SignalR, you could know the connectionId of the user, such as the following code:

    Clients.Client("connectionId").sendMessage("hello word");

    As the same principle, send message to multi users: 

    Clients.Clients(new List<string>() { "connectionId1", "connectionId2",... }).sendMessage("hello word");

    Send message to multi users by group(you could add the connectionId to group before invoke):

    Clients.Group("groupName").sendMessage("hello world");

    I suggest that you could read the tutorial before you using SignalR by the following link.

    https://www.asp.net/signalr

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 5:26 AM
  • User517361227 posted

    Hi EvenMa,

    Thanks for replying, your second code snippet seems fit in my condition, I will go for it and see if it works.

    Thanks
    Vinod K.

    Monday, June 19, 2017 3:57 PM
  • User517361227 posted

    Hi EvenMa,

    I tested this and your second suggestion is exactly what I was looking for

    Clients.Clients(new List<string>() { "connectionId1", "connectionId2",... }).sendMessage("hello word");

    Thanks for help.

    Monday, June 19, 2017 5:50 PM