# Adding headers for Websocket connections

**URL:** <https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786>\
**Category:** Developing Nodes\
**Created:** [27 April 2021 01:57 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786 "2021-04-27T01:57:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chainhead](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chainhead/32/40800_2.png) [@chainhead](https://discourse.nodered.org/u/chainhead)\
**Post date:** [27 April 2021 01:57 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786/1 "2021-04-27T01:57:21Z")

</div>

I am not creating a new node; I am trying to extend the ones for Websocket such that a header is passed before making a `ws` connection.

For my _particular requirement_, I want to read a file and set its content as a header value before the `ws` connection happens. So, in `22-websocket.js`, before `new ws()`, I added the following.

```auto
var someFile = path.join("/tmp", "something")
if (fs.existsSync(someFile)) {
  var someData = fs.readFileSync(someFile)
  options.header = {
    "someHeader": someData
  }
}

```

The `npm test` fails as shown below. Can you please advise?

```bash
  websocket Node
    websocket-listener
      ✓ should load
      ✓ should be server
      ✓ should handle wholemsg property
      ✓ should create socket
      ✓ should close socket on delete
      ✓ should receive data
      ✓ should receive wholemsg
      ✓ should receive wholemsg when data not JSON
      ✓ should receive wholemsg when data not object
(node:4212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 upgrade listeners added to [Server]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
      ✓ should send
      ✓ should send wholemsg
      ✓ should do nothing if no payload (108ms)
      ✓ should echo
      ✓ should echo wholemsg
      ✓ should broadcast
    websocket-client
Fatal error: Cannot read property 'should' of null

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 April 2021 20:41 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786/2 "2021-04-27T20:41:31Z")

</div>

Can't really tell from that test output what is going on. It would be easier probably if you put some console.log output into the code. A link to the original core code would also be helpful - it is a pain to find otherwise 🙂

I assume that you are aware that custom headers are only allowed on a ws connection at the initial handshake? Once the connection upgrades to ws, no custom headers are passed.

---

<div class="post-metadata">

**Author:** ![chainhead](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chainhead/32/40800_2.png) [@chainhead](https://discourse.nodered.org/u/chainhead)\
**Post date:** [28 April 2021 01:33 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786/3 "2021-04-28T01:33:05Z")

</div>

This is the [link](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/network/22-websocket.js) to the source code. (Should have pasted earlier!)

> I assume that you are aware that custom headers are only allowed on a ws connection at the initial handshake? Once the connection upgrades to ws, no custom headers are passed.

The implementation of Websocket nodes uses the `ws` `npm` module. And, I am passing `options.headers` object when `ws` is created. See [here](https://github.com/websockets/ws/blob/HEAD/doc/ws.md#new-websocketaddress-protocols-options) for more details.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 April 2021 20:58 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786/4 "2021-04-28T20:58:01Z")

</div>

> [@chainhead](#):
>
> uses the `ws` `npm` module

Makes no difference, the lack of custom headers past the initial handshake phase is a websockets standard issue. You can find the details on the ws RFC.

* * *

I would think that it would be better to change the core node to allow the passing in of options via an incoming msg. Trying to add your specific requirement to a core node is likely to be very hard to maintain. Whereas allowing options to be passed in would be generally usable for anyone and could be included as an update to Node-RED itself.

The header will still only be available on the initial connection still though.

---

<div class="post-metadata">

**Author:** ![chainhead](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chainhead/32/40800_2.png) [@chainhead](https://discourse.nodered.org/u/chainhead)\
**Post date:** [30 April 2021 15:30 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786/5 "2021-04-30T15:30:40Z")

</div>

> [@TotallyInformation](#):
>
> I would think that it would be better to change the core node to allow the passing in of options via an incoming msg.

Tried that for `websocket-out` node. Same error with `npm test`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 June 2021 15:31 UTC](https://discourse.nodered.org/t/adding-headers-for-websocket-connections/44786/6 "2021-06-29T15:31:06Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
