Skip to content

Commit a5e76bc

Browse files
docs: add example with extraHeaders in the browser
1 parent dac76f2 commit a5e76bc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,41 @@ socket.on('open', () => {
118118
});
119119
```
120120

121+
In the browser, the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) object does not support additional headers.
122+
In case you want to add some headers as part of some authentication mechanism, you can use the `transportOptions` attribute.
123+
Please note that in this case the headers won't be sent in the WebSocket upgrade request.
124+
125+
```js
126+
// WILL NOT WORK in the browser
127+
require('engine.io-client')('http://localhost', {
128+
extraHeaders: {
129+
'X-Custom-Header-For-My-Project': 'will not be sent'
130+
}
131+
});
132+
// WILL NOT WORK
133+
require('engine.io-client')('http://localhost', {
134+
transports: ['websocket'], // polling is disabled
135+
transportOptions: {
136+
polling: {
137+
extraHeaders: {
138+
'X-Custom-Header-For-My-Project': 'will not be sent'
139+
}
140+
}
141+
}
142+
});
143+
// WILL WORK
144+
require('engine.io-client')('http://localhost', {
145+
transports: ['polling', 'websocket'],
146+
transportOptions: {
147+
polling: {
148+
extraHeaders: {
149+
'X-Custom-Header-For-My-Project': 'will be used'
150+
}
151+
}
152+
}
153+
});
154+
```
155+
121156
## Features
122157
123158
- Lightweight

0 commit comments

Comments
 (0)