1

I received data from different server to my Hub class. Each data has its own ID. Whenever data comes to the server hub, it push my data to the client. This is like job progress. I want to send each ID to the client with unique hub id., How do I filter the message from the server? I used in this way Clients.Client("ID1").send(data); Or I have to specify in caller property? Anyone can help me.

With Regards, Shanthini

Shanthini
  • 187
  • 3
  • 10

1 Answers1

3

You can use ConnectionId to identify the client.

When new client is connected, store ConnectionId somewhere so that you can use it later to identify the client.

public class MyHub : Hub
{
    public override Task OnConnected()
    {
        var connectionId = Context.ConnectionId;
        // store connectionId somewhere
        return base.OnConnected();
    }
}

To send data to the client, identify it by ConnectionId:

public void SendNewData(string connectionId, object data)
{
    var Context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
    Context.Clients.Client(connectionId).send(data);
}

If you need to identify clients by some other ID, then you should store relationship between your ID and ConnectionId.

aleyush
  • 644
  • 1
  • 6
  • 19
  • Thank you. I am trying to save my connectionId in the database at the moment. Before that I have another question. All my data coming through the queue. When it hits the server hub, it push the data to the client. Now, how do I know which data is hitting my hub? Hub receives the data from the queue; Its a continuous process. Come, hit and display., Then how can I filter each data? Is that make sense? – Shanthini May 24 '13 at 10:12
  • Not sure I understand your question. Consider hub as a way to run a remote procedure with passing some data as parameters (client procedure from server or server procedure from client). All the other is related to your application logic (when to call the procedure, what should this procedure do with the data recieved, etc). – aleyush May 24 '13 at 13:52
  • Oh thank you very much., I have done my requirement by using your answer :) – Shanthini May 28 '13 at 11:46
  • This fails form me when i"m trying to call my own connection id – ECie Feb 02 '15 at 06:59