-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing query parameter issue #1677
Comments
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)
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? |
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. 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 Also, I don't see a query property used in the code... Are you sure this is supported? |
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. 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. |
The fix that would solve my situation is not released yet. |
I can't believe that this issue exist and is not implemented just from the begining .. |
The same problem for me :( |
+1 |
4 similar comments
+1 |
+1 |
+1 |
+1 |
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". |
+1 |
any update on this issue ? still exist with v1.4.8 |
+1 |
1 similar comment
+1 |
…to the timestamp as a query parameter and this: socketio/socket.io#1677
+1 |
1 similar comment
+1 |
Any update on this issue ? |
"socket.io-client": "2.1.1" Ran into this problem myself today and I think I may have found a solution. The solution as proposed in the client query object documentation did not work for me ( // 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 {
"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 Yes, but it is updated only once. |
This function works for me:
I am on socket.io-client |
Why isn't this fixed after six years? 🤔 Is there any way to change the query without calling |
Why dont you fix this? 🤔 This is an open source project... |
The problem is that the The solution by @GStipick will only work for a Socket in a non-default namespace: const socket = io('/admin', {
query: { abc: 'def' },
});
socket.on('reconnect_attempt', () => {
socket.query = {
token: 'ghi'
}
}); For the default namespace, you must update the const socket = io({
query: { abc: 'def' },
});
socket.on('reconnect_attempt', () => {
socket.io.opts.query = {
token: 'ghi'
}
}); This change will only be used upon reconnection to the namespace (hence the This inconsistency will be fixed in Socket.IO v3, please see my proposition here: #3250 (comment) |
For future readers: this was fixed in Socket.IO v3 Documentation: |
I'm getting something similar in 4.0.1 |
close the socket before change query
test in version 4 |
@bij7n a minor nitpick, in your example above, the socket.disconnect()
socket.io.opts.query={key:valye}
socket.connect() Reference: https://socket.io/docs/v4/client-api/#socketclose Thanks for the code example 👍 |
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 timesocket.io.opts.query._accessToken = 'moo';
Server's socket.request._query._accessToken returns 'moo' ##### Iteration 2 - Doesn't work anymoresocket.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.
The text was updated successfully, but these errors were encountered: