test
test

Reputation: 2618

hubcontext is null signalr

At the moment I have learned how to notify the hub from the serverside. The hub then broadcasts the notification to the client.

My context that I am working on is based on sending a friend request (similar to facebook). Where if Mr X sends a request to Ms Y, then only Ms Y will have a notification pop up.

Thus I decided to work with Groups. I am thinking one group - one user.

The problem is that I tried that on hub start i would do:

$.connection.hub.start(function () { hub.login(XXX);});

where XXX would be the username or userID from the session. I don't know how to do this.

I then tried to make a hidden label with the username or userID, fill it on page_load and then take it from the client side. But client side works before server side.

I then tried to use the connection id of the hub context. But the problem seems to be that almost everything such as context and group manager are null in the hub. Is it because I am using an instance of the hub? or because I have static methods? (which I need to have static/ or else have an instance since I am calling these methods from other server side code)

     using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SignalR.Hubs;
using SignalR;
using SignalR.Hosting.AspNet;
using System.Web;
using System.Web.SessionState;

namespace DataLayer
{
    public class NotificationHub: Hub
    {
        private static NotificationHub instance;

        private NotificationHub() { }

        public static NotificationHub Instance
        {
            get
            {
                if (instance == null)
                    instance = new NotificationHub();
                return instance;
            }
        }

        public void NotifyClients(int gid, string value)
        {
            IConnectionManager connectionManager = (IConnectionManager)AspNetHost.DependencyResolver.GetService(typeof(IConnectionManager));
            dynamic clients = connectionManager.GetClients<NotificationHub>();
            clients[gid.ToString()].newNotification(value);
            //let us then try to use connection id, but this.Context.ConnectionId is null

        }

        public void Login(string gid)
        {
            AddToGroup(gid);
        }

    }
}

May be there is some other workaround to what I would like to do? (i.e. letting each person have their own notifications?)

Upvotes: 2

Views: 3266

Answers (1)

drch
drch

Reputation: 3080

Your js reference to the hub should be $.connection.notificationHub not $.connection.hub . It's taken from the classname of your hub. You can rename it with the [HubName("name")] attribute if you like.

Additionally, SignalR will manage the lifecycle of your Hub. You don't need to create the instances. I'd double check the source to see if its per-call or singleton etc.

With respect to (if I'm reading correctly) using the connection id of the friend in any way - that's not a good idea. At the moment, they are not secured and that would allow a user to masquerade as any other user quite easily.

Edit: one more tip - within the hub, you don't have to use the ConnectionManager to get the clients. You can just use the Clients property. IE, clients[gid.ToString()].newNotification(value);

Upvotes: 4

Related Questions