Usage of websockets
johanvdw opened this issue · comments
As this issue is closed:
#13
I wonder what's the correct method to use websockets.
Here is what I tried in my rollup.config.js:
plugins: [
!production && dev({
extend: fp(async (server) => server.register(require("@fastify/websocket"))),
basePath: '/live',
dirs:['public'],
onListen(server) { server.log.info('Hello world')},
proxy: [{from: '/metadata/', to: 'http://localhost:5001/metadata/'},{from: '/temperature/', to: 'http://localhost:5001/temperature/', opts: { websocket: true}}]
}
),
When trying to access the endpoint with a websocket (/temperature)
status code remains: '101 Switching Protocols'
All I can see from the root Fastify implementation is your upstream shouldn't be http
protocol but ws
.
Have you tried: to: 'ws://localhost:5001/temperature/'
?
I tried that, but then the proxy gives a 501 (invalid frame header).
I've added a test for websocket proxying here - #28
I don't think you need extend
in your config, Fastify's http-proxy already ships that as a dependency.
If it's still not working after removing that I can't do much to help.
Thanks, without the extend, things looks better now. I did notice that the websocket is now binary rather than text. I'll try to handle this.