engine.io-client icon indicating copy to clipboard operation
engine.io-client copied to clipboard

Add negotiate

Open zackliu opened this issue 2 years ago • 5 comments

Note: the engine.io.js file is the generated output of make engine.io.js, and should not be manually modified.

The kind of change this PR does introduce

  • [ ] a bug fix
  • [x] a new feature
  • [ ] an update to the documentation
  • [ ] a code change that improves performance
  • [ ] other

Current behaviour

The engine.io connection directly connects to the hostname, path.

New behaviour

If negotiatePath is set, engine.io will make a fetch to the <hostname>/<negotiatePath> to get a JSON encoded response first.

{
  "url", "http://hostname/path",
  "token": "<token>"
}

if succeeded, the engine.io will parse the url got in negotiation and replace it with the previous hostname, path, port and protocol in opts. As the opts has changed, everything is unchanged then (e.g when constructing a transport, it still read from opts).

Another change is adding a token in opts. If token is set, it will be used in Authorization for long polling, and access_token query for websocket. This token can be set in opts directly but it can also be override in negotiation.

Other information (e.g. related issues)

This is the implementation of issue https://github.com/socketio/socket.io/issues/4825

zackliu avatar Sep 19 '23 11:09 zackliu

@darrachequesne We can discuss whether the design is acceptable, and I will write some more test.

zackliu avatar Sep 21 '23 06:09 zackliu

@darrachequesne Do you have any comments on the design?

zackliu avatar Oct 12 '23 05:10 zackliu

Hi @zackliu,

Thanks for your work on this.

However, I still don't think the use case is common enough for this to be included here in the package.

darrachequesne avatar Oct 13 '23 12:10 darrachequesne

@darrachequesne I think there're two parts in it.

  1. Added a token in opts, which can be used in Authorization in long polling and access_token query in websocket
  2. Add a negotiate opts and replace the correlating fields in opts.

I admit we can use a separate library for the second purpose but what about the first one? Do you think it's acceptable to have such a property as it's hard to achieve outside this package?

zackliu avatar Oct 23 '23 08:10 zackliu

For 1, the transportOptions option should be sufficient I think:

import { io } from "socket.io-client";

const socket = io({
  transportOptions: {
    polling: {
      extraHeaders: {
        Authorization: myToken
      }
    },
    websocket: {
      query: {
        access_token: myToken
      }
    }
  }
});

Reference: https://socket.io/docs/v4/client-options/#transportoptions

darrachequesne avatar Oct 23 '23 09:10 darrachequesne

I think this can be closed now. Please reopen if needed!

darrachequesne avatar Jun 24 '24 07:06 darrachequesne