Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - Calling members of a group not working

Context

I'm using SignalR 3 RC1 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 ?

like image 670
Nicolas Law-Dune Avatar asked Oct 30 '22 10:10

Nicolas Law-Dune


1 Answers

For people interested in using ASP.NET 5 with Signalr 2.2, i create a bridge between IAppBuilder and IApplicationBuilder

internal static class IApplicationBuilderExtensions
    {
        public static void UseOwin(
          this IApplicationBuilder app,
          Action<IAppBuilder> owinConfiguration)
        {
            app.UseOwin(
              addToPipeline =>
              {
                  addToPipeline(
                    next =>
                    {
                        var builder = new AppBuilder();

                        owinConfiguration(builder);

                        builder.Run(ctx => next(ctx.Environment));

                        Func<IDictionary<string, object>, Task> appFunc =
                          (Func<IDictionary<string, object>, Task>)
                          builder.Build(typeof(Func<IDictionary<string, object>, Task>));

                        return appFunc;
                    });
              });
        }
    }

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
            app.UseOwin(owin => owin.MapSignalR());
}

Importing these dependencies

"Microsoft.AspNet.Owin": "1.0.0-rc1-final",
"Microsoft.Owin": "3.0.1"
like image 108
Nicolas Law-Dune Avatar answered Jan 04 '23 13:01

Nicolas Law-Dune