socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Changing query parameter issue

Open TheBubbleDuck opened this issue 12 years ago • 29 comments

I am hoping I can get some direction on this.

Initializing the io with io(url,opts) works with the query parameter. If that query parameter needs to change you can dot into the sio.io.opts object. However, after it is changed that way, it is not able to be changed again.

Iteration 0 - Works On Initialization
socket = io(options.socket,{query: {_accessToken: 'cow'} }); Server's socket.request._query._accessToken returns 'cow' ##### Iteration 1 -Works When changed the first time socket.io.opts.query._accessToken = 'moo'; Server's socket.request._query._accessToken returns 'moo' ##### Iteration 2 - Doesn't work anymore socket.io.opts.query._accessToken = 'twomoos'; Server's socket.request._query._accessToken returns 'moo''

Although it seems to change on the client side, the server still returns the value of the access token in Iteration 1.

Doing some digging, I found that the socket.io.engine.transport.ws contains the URL for the request, but keeps the _accessToken as the iteration 1 variable. ws://192.168.1.8/socket.io/?_accessToken=moo&transport=websocket&sid=mY9rC4km7ASypgDTAAAI I can't change the variable using javascript because it still stays the same. That also seems rather hacky and I'd like to not do that.

I haven't put in an issue request for anything, so I hope this counts as a legitimate one.

TheBubbleDuck avatar Jul 10 '14 17:07 TheBubbleDuck

So I found one of the ways to resolve this as a workaround is to create a new io.Manager and pass in the new token (after I do socket.destroy() on the original object as a test) socket = io(options.socket);

socket.io = new io.Manager(options.socket,{query: {_accessToken: $auth.getToken()} });

This seems to effectively reconnect and change the token each time (or any other query params). I don't know if this is what you are supposed to do since most articles I read suggest changing the opts.query

Hoping to get some insight or see if we can find if its a bug?

TheBubbleDuck avatar Jul 24 '14 20:07 TheBubbleDuck

I've been providing the query string through the url (instead of through a query property of the options object). And I've been experiencing that it doesn't pick up query changes at all. Looking through the code it doesn't pass changes properties to a cached manager. (Unless the namespace changed or options like forceNew, or multiplex are used). https://github.com/Automattic/socket.io-client/blob/master/lib/index.js#L36

I can confirm this by enabling the debug for engine.io* and socket.io*. I see that eventhough I changed the querry string in the url it opens the old url. (Through the socket.io-client:manager opening ... statement)

Also, I don't see a query property used in the code... Are you sure this is supported? You are using version 1.x and not 0.9 right? Maybe you can also debug this by enabling the debug statements of socket.io and engine.io.

peteruithoven avatar Feb 17 '15 13:02 peteruithoven

A bigger issue for me is that I can't work around this with forceNew, without always using it (disabling multiplexing in general). It will pick up query string changes when forceNew is enabled, but when I don't supply this anymore it will fallback to the old & cached manager and it will use the old query string again. This is partly caused by the fact that a connection made with forceNew or disabling multiplex are not cached.

Actually I now see that it should make a new connection when it's the same namespace since a few days, see the commit below. That would solve my issue. Maybe this is not released yet. I'll check.
https://github.com/Automattic/socket.io-client/commit/7d237cc91fbb34ce484b188cf7d10462c6b34bd9 Also see: https://github.com/Automattic/socket.io/issues/1956

peteruithoven avatar Feb 17 '15 14:02 peteruithoven

The fix that would solve my situation is not released yet.

peteruithoven avatar Feb 17 '15 16:02 peteruithoven

I can't believe that this issue exist and is not implemented just from the begining ..

ivaylopivanov avatar Mar 16 '15 11:03 ivaylopivanov

The same problem for me :(

Aliance avatar Nov 18 '15 13:11 Aliance

+1

abaranovskii avatar Nov 18 '15 14:11 abaranovskii

+1

lunow avatar Nov 21 '15 14:11 lunow

+1

rek avatar Jan 20 '16 09:01 rek

+1

twong avatar Jan 20 '16 09:01 twong

+1

FredLuart avatar Jan 21 '16 15:01 FredLuart

On socket.io 1.4.4, even with forceNew parameters, query modification using "socket.io.opts.query" is not considered on reconnection. This is working with version 0.9 using "socket.socket.options.query".

FredLuart avatar Jan 25 '16 20:01 FredLuart

+1

pciazynski avatar Apr 12 '16 08:04 pciazynski

any update on this issue ? still exist with v1.4.8

FredLuart avatar Jun 24 '16 07:06 FredLuart

+1

AlexDanault avatar Aug 26 '16 11:08 AlexDanault

+1

ryanthegiantlion avatar Sep 20 '16 06:09 ryanthegiantlion

+1

juanpujol avatar Nov 14 '16 18:11 juanpujol

+1

codeofsumit avatar Nov 21 '17 10:11 codeofsumit

Any update on this issue ?

FredLuart avatar Mar 15 '18 13:03 FredLuart

"socket.io-client": "2.1.1"

Ran into this problem myself today and I think I may have found a solution. I was able to change the query parameter option by updating the mysocket.query object.

The solution as proposed in the client query object documentation did not work for me (mysocket.io.opts.query did not change the query param for the socket)

// On socket creation
const authToken = localStorage.getItem('authToken');
const mysocket = io(`${serverUrl}/${namespaceName}`, {
  transports: ['websocket'],
  query: { authToken },
});

// Somewhere else in my code after I have updated the localStorage
const authToken = localStorage.getItem('authToken');
mysocket.query = { authToken }

// Next time I 're-connected' the socket, the query param was updated correctly and the new authToken was sent

The mysocket object looks as follows:

{
  "io": {},
  "json": {},
  "nsp": "/Test",
  "ids": 0,
  "id": "/Test#randomString",
  "acks": {},
  "receiveBuffer": [],
  "sendBuffer": [],
  "connected": false,
  "disconnected": true,
  "flags": {},
  "query": { "authToken": "Whaterver Your Auth Token Is"}, // <-- I was able to update this directly
  "subs": null,
  "_callbacks": { "$connecting": [null], "$connect": [null], "$action": [null] }
}

Hopefully this helps.

GStipick avatar Oct 08 '18 19:10 GStipick