Context

I'm using SignalR3 with ASP.NET 5 for my project and i'm having trouble getting my client subscribed to a specific group, to receive message from my server.

Hub class

[HubName("ChatHub")]
public class ChatHub : Hub
{
   public async Task Join(string userId)
   {
      if (!string.IsNullOrEmpty(userId))
      {
          await Groups.Add(Context.ConnectionId, userId);
      }
   }
}

JS code

var chatHub = hubConnection.createHubProxy("chatHub");
    chatHub .on("newMessage", (response) => {
        console.log(response);
    });

    hubConnection.start().done(response => {
        chatHub.invoke("join", "userid");
    });

WebApi

public class ChatController : ApiController
{
    protected readonly IHubContext ChatHub;

    public ChatController(IConnectionManager signalRConnectionManager)
    {
        ChatHub = signalRConnectionManager.GetHubContext<ChatHub>();
    }

    [Authorize]
    [HttpPost]
    [Route("Message")]
    public async Task<IActionResult> CreateMessage([FromBody] messageParams dto)
    {
        await ChatHub.Clients.Group("userid").NewMessage("hello world");
    }
}

if I broadcast "All" clients, it's working.

await ChatHub.Clients.All.NewMessage("hello world");

Is there a specific configuration to broadcast message to specific group ?

Related posts

Recent Viewed