7

Is it possible to change querystring on a SignalR connection without perform a stop and start?

I´m doing this:

$.connection.hub['qs'] = {arg1: 'kkkk', arg2: 'xxx',...};
$.connection.hub.stop();
$.connection.hub.start();

This stop/start is causing my system to interpret it is as a connection break up, what is undesirable for many reasons.

When I remove it, methods are invoked with older querystring arguments.

2
  • Do you explore any solution for this problem then reconnect? Commented Aug 8, 2016 at 12:25
  • I used the solution below, basically breaking the connection at the "transport layer" bypassing the connection events trigger. Commented Aug 10, 2016 at 20:00

1 Answer 1

9

The code you have to update the query string ($.connection.hub.qs = {arg1: 'kkkk', arg2: 'xxx',...};) will be reflected in the query string of any future requests SignalR makes for that connection.

In the case of anything but the WebSocket transport, i.e. server-sent events, forever frame, and long polling, any subsequent server hub invocation should be made by an Ajax request with the updated querystring.

However, if WebSockets is in use, there shouldn't ever be a subsequent request to attach the updated query string to. At least this is the case if the connection doesn't die forcing a reconnect.

One solution is to restart the connection like you did. It would be better yet if you would set the query string before you start the connection in the first place.

If you really need to change the query string after the connection starts and you don't want the system to interpret this as a brand new connection, you might be able to do something like the following:

$.connection.hub.qs = {arg1: 'kkkk', arg2: 'xxx',...};
$.connection.hub.transport.lostConnection($.connection.hub);

This will basically trick the transport into thinking the connection is dead and cause it to reconnect with the updated query string without renegotiating and connecting with a new ConnectionId. The problem with this approach is that the second line uses an undocumented API.

Sign up to request clarification or add additional context in comments.

1 Comment

As that kind of query string change is relevant only for old transports (long pooling, sse, etc), It´s not a problem for my cases when the user is connected through web sockets. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.