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

Support for additional HTTP headers in Websocket connection #856

Closed
rafaelubal opened this issue Aug 22, 2018 · 7 comments
Closed

Support for additional HTTP headers in Websocket connection #856

rafaelubal opened this issue Aug 22, 2018 · 7 comments
Labels

Comments

@rafaelubal
Copy link

rafaelubal commented Aug 22, 2018

Hi,

I'm trying to add additional authentication headers in the initial HTTP connection request, but using option wsOptions does not seem to add additional headers:

client = mqtt.connect(url, {
  clientId: client_id,
  wsOptions: {
    headers: {
      'My-Header': 'my_value'
    }
  }
});

If there is currently a way to do this, it'd be great if the documentation explicitly stated it. If not, I'd greatly appreciate support for this feature. Thanks a lot!

Rafael

@mcollina
Copy link
Member

I don't think that's currently supported. Would you like to send a PR?

@rafaelubal
Copy link
Author

I took a quick look at the code, and it looks like a limitation of the underlying 'websocket-stream' library used to create the WebSocket connection. In fact, it appears to be a restriction of the native WebSocket class specification on the browser (https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). However, the Amazon IoT Device SDK finds a way to do this, I'm not sure how at the moment.

@phillipj
Copy link

phillipj commented Sep 4, 2018

I spent a day digging deep into getting an AWS IoT custom authorizer working, which requires custom Websockets HTTP headers. In short I stumbled upon this issue where AWS employee @AWSSteveHa confirms several times that it can't be done from browsers because of limitations in the underlying Websocket implementation: aws/aws-iot-device-sdk-js#169

However, the Amazon IoT Device SDK finds a way to do this, I'm not sure how at the moment.

Ohh! Any hints how to make that work? Would be really interesting digging into the "how" there..

@rrrhys
Copy link

rrrhys commented Nov 14, 2018

To send the headers in the connect request, please try installing a modified websocket-stream library.
npm install --save git+https://github.com/rrrhys/websocket-stream.git

I've modified websocket-stream to pass through headers when supplied in the options (for a React Native project).

@aler9
Copy link

aler9 commented Aug 6, 2019

Hi, i'm interested in this feature and after some testing i can say that:

  • additional headers DO WORK when the library is used in a server-side Node script, with the exact same code posted by @rafaelubal
  • additional headers DO NOT WORK when the library is used in the browser
  • i have no data regarding React Native, but the solution proposed by @rrrhys seems to implement the feature for this specific platform

@github-actions
Copy link

This is an automated message to let you know that this issue has
gone 365 days without any activity. In order to ensure that we work
on issues that still matter, this issue will be closed in 14 days.

If this issue is still important, you can simply comment with a
"bump" to keep it open.

Thank you for your contribution.

@github-actions github-actions bot added the stale label Oct 10, 2022
@github-actions
Copy link

This issue was automatically closed due to inactivity.

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

No branches or pull requests

5 participants