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

Manipulate the URL #127

Open
Tockra opened this issue Apr 16, 2024 · 1 comment
Open

Manipulate the URL #127

Tockra opened this issue Apr 16, 2024 · 1 comment

Comments

@Tockra
Copy link

Tockra commented Apr 16, 2024

Hi folks,

I've the problem that my websocket endpoint needs authentication.
The first approach was to send a Authorization header field with the bearer token like ALL my endpoints expect.
But I found no websocket client for javascript providing a custom header field.
So I changed to queryParameter. So my URL contains ws://<baseURL>/<path>?token=<access_token> .

Now I ran into the next problem. If I lose the connection to my websocket for longer than 5 minutes, then the access_token stored in the initial URL will be invalid. In this case the re-connect logic results in a infinite loop.

So I would like to adjust the websockets URL in case my auth.access_token changes.
Is there currently a mechanism allowing that?

Otherwise I have to kill my websocket connection each 5 minutes and restore it. This would no so cool.

T

@imlinhanchao
Copy link

I think you can listen to the "close" event to check the token. If the token is invalid, force close and reconnect.

In addition, if you want to use custom headers in the websocket connection, you can do it by customizing the Websocket class, for example:

function customWebSocketWithHeaders(headers)
  class MyWebSocket extends WebSocket {
    constructor(url, protocols) {
      super(url, protocols, { headers });
    }
  }
  return MyWebSocket;
}

const rws = new ReconnectingWebSocket(wsUrl, [], {
  WebSocket: customWebSocketWithHeaders({ your_header: 'value'}),
})

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

2 participants