Skip to content

Commit 17ac3a2

Browse files
authored
Pass WebSocket subprotocol to upstream (#256)
1 parent 2ffab3d commit 17ac3a2

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ A few things are missing:
165165
1. forwarding headers as well as `rewriteHeaders`. Note: Only cookie headers are being forwarded
166166
2. request id logging
167167
3. support `ignoreTrailingSlash`
168+
4. forwarding more than one subprotocols. Note: Only the first subprotocol is being forwarded
168169

169170
Pull requests are welcome to finish this feature.
170171

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ function setupWebSocketProxy (fastify, options, rewritePrefix) {
9090
return
9191
}
9292

93+
const subprotocols = []
94+
if (source.protocol) {
95+
subprotocols.push(source.protocol)
96+
}
97+
9398
let optionsWs = {}
9499
if (request.headers.cookie) {
95100
const headers = { cookie: request.headers.cookie }
@@ -100,7 +105,7 @@ function setupWebSocketProxy (fastify, options, rewritePrefix) {
100105

101106
const url = createWebSocketUrl(request)
102107

103-
const target = new WebSocket(url, optionsWs)
108+
const target = new WebSocket(url, subprotocols, optionsWs)
104109

105110
fastify.log.debug({ url: url.href }, 'proxy websocket')
106111
proxyWebSockets(source, target)

test/websocket.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ const { createServer } = require('http')
88
const { promisify } = require('util')
99
const { once } = require('events')
1010
const cookieValue = 'foo=bar'
11+
const subprotocolValue = 'foo-subprotocol'
1112

1213
test('basic websocket proxy', async (t) => {
13-
t.plan(3)
14+
t.plan(4)
1415

1516
const origin = createServer()
1617
const wss = new WebSocket.Server({ server: origin })
1718
t.teardown(wss.close.bind(wss))
1819
t.teardown(origin.close.bind(origin))
1920

2021
wss.on('connection', (ws, request) => {
22+
t.equal(ws.protocol, subprotocolValue)
2123
t.equal(request.headers.cookie, cookieValue)
2224
ws.on('message', (message) => {
2325
t.equal(message.toString(), 'hello')
@@ -38,7 +40,7 @@ test('basic websocket proxy', async (t) => {
3840
t.teardown(server.close.bind(server))
3941

4042
const options = { headers: { cookie: cookieValue } }
41-
const ws = new WebSocket(`ws://localhost:${server.server.address().port}`, options)
43+
const ws = new WebSocket(`ws://localhost:${server.server.address().port}`, [subprotocolValue], options)
4244

4345
await once(ws, 'open')
4446

@@ -47,7 +49,6 @@ test('basic websocket proxy', async (t) => {
4749
stream.write('hello')
4850

4951
const [buf] = await once(stream, 'data')
50-
5152
t.equal(buf.toString(), 'hello')
5253

5354
await Promise.all([

0 commit comments

Comments
 (0)