0

If I store a connection id associated with an order in my DB, can I use that connection id to reopen the same SignalR connection when the page is refreshed.

Is there a way to specify a connection id to open when calling $.connection.hub.start() ?

Something like $.connection.hub.start("001ddf34-23sd-nds7d-asdjh-647hfash3")

user95227
  • 1,853
  • 2
  • 18
  • 36
  • Why do you store the connection ID? Why not the user? – mason Feb 15 '16 at 20:54
  • @mason thank you for reaching out. It's my first time using SignalR. How can I get the user so that I can store it (I take it you're referring to a user piece of the connection context and not my customer entity)? And is it possible to reopen a connection to that user like I am wishing to do with a connection id? I will google these things now but if you know the syntax to get the user or open a connection to that user, please post it. – user95227 Feb 15 '16 at 21:06
  • 1
    The way I've seen it implemented is that you talk to a user via `Clients.User(userId).doSomething()`. This will call `doSomething` on all clients that are connected with that `userId`. – mason Feb 15 '16 at 21:22
  • 1
    When a page closes/refreshes the client stops the connection and the server no longer keeps track of the client. To connect to the server you need to start a connection and during this process the server assigns a new connection id to the client. Instead of trying to circumvent this you could pass some additional parameter to the server to correlate the same client across multiple connection ids. – Pawel Feb 15 '16 at 21:27
  • @mason Thanks again. Do you know of a way to get the userId from the client? Previously, I was using `$.connection.hub.id` to get the connection id from javascript. – user95227 Feb 15 '16 at 21:39
  • 1
    I think you really should just forget about the connection ID's. In your hub, have a look at `Context.User.Identity.Name`. – mason Feb 15 '16 at 21:51
  • @mason will do. I'm going to try to use the user id as you suggested. However my context does not have a `.User` property so `Context.User.Identity.Name` throws an error. I must be missing something. Any ideas? Thank you very much for your patience. I am currently trying `var hubContext = GlobalHost.ConnectionManager.GetHubContext(); string name = hubContext.User.Identity.Name; hubContext.Clients.User(name).send("Test Message");` – user95227 Feb 15 '16 at 22:02
  • 1
    In that case, you're not *within* the hub. You're just getting the context of the hub. Where are you when you call that? An MVC controller? A background process? A Web Forms code behind? – mason Feb 15 '16 at 22:04
  • @mason I'm in an Api controller. When you say within the hub, do you mean in the MyAppHub class itself? Thanks – user95227 Feb 15 '16 at 22:30
  • 1
    Yes, when I say within the hub, I mean within an instance method of the hub class. But assuming your UserId's in Web API lined up with the UserId's in SignalR, you can [get your current username from Web API](http://stackoverflow.com/questions/21616951/get-the-current-user-within-an-apicontroller-action-without-passing-the-userid) and use that. And if the users aren't matching up, see [Mapping SignalR Users to Connections](http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections). – mason Feb 15 '16 at 22:33
  • @mason Ok that makes sense. The user's are not matching up (id and username from `RequestContext.Principal` are empty). I will try to follow the instructions on the guide you linked me to for mapping them tomorrow. Thanks again! – user95227 Feb 15 '16 at 22:46
  • @mason I would like to map connections to orders, not users. Think I could map orderId's to the request user id the same way your link maps custom user names? – user95227 Feb 16 '16 at 16:44
  • Your order table should already have a relationship to a user, right? So I really see it as you just need to make sure SignalR has the same UserId or a way to go from the SignalR UserId to the Order UserId. – mason Feb 16 '16 at 17:51

0 Answers0