Search code examples
asp.netangularjsasp.net-web-apisignalrsignalr-hub

SignalR client specific with angularjs


So I am fairly new with signalR and had worked with it a bit with MVCs. Now I am using it in webapi with angularjs and am a bit confused or have forgotten of what I have done. I am using bearer tokens with webapi and am trying to create a notification system.

What I want to figure out is the proper way of using angularjs with signalR. I see many people use the proxy on/invoke. Is the proxy.on is when I call the hubcontext from the server as so:

IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(); hubContext.Clients.User(UserId).broadcastNotification("Good morning! The time is " + DateTime.Now.ToString());

and the proxy.invoke method is from the client side? If so, which would be the best way for using notification systems (I would assume the proxy.on)?

My second question is more on sending notifications to specific users. For sending requests to specific users, I would assume you would want to do this on the hub as so:

public void SendNotification(string userId) { Clients.User(userId).broadcastNotification("Good morning! The time is " + DateTime.Now.ToString()); }

My startup is something like this:

 public class MyProvider : IUserIdProvider
    {
        public string GetUserId(IRequest request)
        {
            var userId = request.User.Identity.Name;
            return userId.ToString();
        }
    }
    public void Configuration(IAppBuilder app)
    { 
        HttpConfiguration config = new HttpConfiguration();
        ConfigureOAuth(app);
        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<AuthContext, Travelfy.API.Migrations.Configuration>());
        GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new MyProvider());
        app.MapSignalR("/hubs", new HubConfiguration());

    }

When I refresh my pages, I notice that all my userids are all empty strings "". I was reading that maybe it was due to using bearer tokens. If so, how would I use bearer tokens to specific the userId that I would want to send to? When I use the Clients.All everything works fine, so I'm assuming it has to be something with the startup/userIds I am getting.

Thanks


Solution

  • So after a while, I was able to figure out the right answer. Because I was using bearerTokens, I really had to determine another method of obtaining the userId rather than just relying on request.User.Identity.Name. What I needed to do was pass my bearerToken to the connection.qs value.

    connection.qs = { Bearer: token };

    Once I was able to do that I had to route the find my user based on the token that I had sent in.

    var token = request.QueryString.Get("Bearer"); var authenticationTicket = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(token);