Socket.io-client: EXTRA HEADER with Websocket only

Created on 21 Apr 2020  ·  7Comments  ·  Source: socketio/socket.io-client

Extra header is currently not supported with websocket only connection. In the documentation you said the RFC 6455 doesn't "honour it".
If you look at page 22/ 10
https://tools.ietf.org/html/rfc6455#page-22

  1. Optionally, other header fields, such as those used to send
    cookies or request authentication to a server. Unknown header
    fields are ignored, as per [RFC2616].

Clearly it supports it, and I personally find it very useful too. Also it is not that complicated with the ws library that engine.io uses.
Any support? Thanks

question

Most helpful comment

Hey guys.
It did not work.
I have just tested it in my integration tests, did not work.

All 7 comments

https://github.com/socketio/engine.io-client/blob/27fa6949f38896e18a6ef426516359f8d54e7db6/lib/socket.js#L124

https://github.com/socketio/engine.io-client/blob/master/lib/transports/websocket.js#L63

By read source code mentioned above, you can make this work by:

socketIO('https://example.org', {
  path: '/api/endpoint',
  transports: ['websocket'],
  transportOptions: {
    websocket: {
      extraHeaders: {
        Cookie: 'It works',
      },
    },
  },
});

And even this:

socketIO('https://example.org', {
  path: '/api/endpoint',
  transports: ['websocket'],
  extraHeaders: {
    Cookie: 'It works',
  },
});

Based on the source code snippets indeed looks like there is no restriciton, but did you test it? I am going to test it later when I have time, but if it works then the documentation needs update, since:

https://socket.io/docs/client-api/#With-extraHeaders:

With extraHeaders
This only works if polling transport is enabled (which is the default). Custom headers will not be appended when using websocket as the transport. This happens because the WebSocket handshake does not honor custom headers. (For background see the WebSocket protocol RFC)

const socket = io({ transportOptions: { polling: { extraHeaders: { 'x-clientid': 'abc' } } }});

It states otherwise...

Hey guys.
It did not work.
I have just tested it in my integration tests, did not work.

@najibghadri @behruzz It works for me with above example, and don't put extreHeader under polling

It works for me when runninng in nodejs, it does not when running in chrome. This cannot be done as it is not supported by the browsers api https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
I need the header for my load balancer to correctly deliver the request to the socket server, but I gues we will need a different mechanism

It seems that somebody thinks it should work, since also according to the documentation:

extraHeaders | {} | Headers that will be passed for each request to the server (via xhr-polling and via websockets). These values then can be used during handshake or for special proxies.

By the way, if you can't use cookies, does this basically mean you can only use IP addresses for sticky balancing?

@AvailCat please note that the examples you provided will not work in the browser

@rotvr if you only use the WebSocket transport, you don't need sticky session, since the WebSocket connection (and the underlying TCP connection) is kept open during the whole session.

If you do use the polling transport, the first HTTP request sets a cookie (io), which can be used for sticky-session.

More information here: https://socket.io/docs/using-multiple-nodes/

See also: https://github.com/socketio/engine.io-client/issues/635#issuecomment-638713082

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yesterday24 picture yesterday24  ·  4Comments

Korri picture Korri  ·  8Comments

catamphetamine picture catamphetamine  ·  3Comments

Sairyss picture Sairyss  ·  4Comments

exilonX picture exilonX  ·  7Comments