Skip to content
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

Why overwrites query by handshake? #3592

Closed
imcuttle opened this issue May 1, 2020 · 3 comments
Closed

Why overwrites query by handshake? #3592

imcuttle opened this issue May 1, 2020 · 3 comments
Milestone

Comments

@imcuttle
Copy link

imcuttle commented May 1, 2020

When I use socket.io multiplexing feat, setup ws connection by query {type: ''}, and then I updated query {type: 'share'}, but it not received in server side, I saw code as follow

socket.io/lib/socket.js

Lines 116 to 120 in 2a1aa1c

function buildQuery(){
var requestQuery = url.parse(self.request.url, true).query;
//if socket-specific query exist, replace query strings in requestQuery
return Object.assign({}, query, requestQuery);
}

So why overwrites query by handshake? And now I use forceNew=true to disable multiplexing feat

@sebamarynissen
Copy link
Contributor

It's a bug that I've encountered as well. I still need to test if it's fixed in v3, but have a look at #3495 for a workaround in v2.

@darrachequesne
Copy link
Member

Yes, that's a known quirk in Socket.IO v2, which should be fixed in v3. The explanation can be found in the migration guide: https://socket.io/docs/migrating-from-2-x-to-3-0/#Add-a-clear-distinction-between-the-Manager-query-option-and-the-Socket-query-option

As a workaround for Socket.IO v2, you can explicitly create your Socket instance on the client-side.

Instead of:

const socket = io({
  query: {
     token: "abc"
  }
});

You can write:

const { Manager } = require("socket.io-client");

const manager = new Manager();

const socket = manager.socket("/", {
  query: {
    token: "abc"
  }
});

In this case, you can update the socket.query object, it should be reflected on the server-side upon reconnection (and not overriden by the Manager query option):

// client-side
socket.query.token = "def";

// server-side
io.on("connect", (socket) => {
  console.log(socket.handshake.query.token); // def
});

@darrachequesne darrachequesne added this to the 3.0.0 milestone Nov 8, 2020
@sebamarynissen
Copy link
Contributor

sebamarynissen commented Nov 8, 2020

@darrachequesne Would there be a possibility to merge #3496 into the 2.4.x branch? It looks to me that the problem is simply that the http query overrides the socket specific query, while the socket specific query should actually override the http query instead (see #3495 (comment)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants