fastify / fastify-websocket

basic websocket support for fastify
MIT License
394 stars 72 forks source link

connections is of type _Request while request is of type Reply #277

Closed raythurnvoid closed 10 months ago

raythurnvoid commented 10 months ago

Prerequisites

Fastify version

4.24.3

Plugin version

8.2.0

Node.js version

20.8.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Description

I was struggling to get the query string from the request because the handler parameters don't match with what they are supposed to be according to the docs:

image

Steps to Reproduce

import Fastify from 'fastify';
import websocket from '@fastify/websocket';
import { WebSocket } from 'ws';

const fastify = Fastify();
fastify.register(websocket);

fastify.get('/chat', { websocket: true }, (connection, req) => {
    try {
        console.log(connection, req);
    } catch (e) {
        console.error(e);
    }
});

fastify.listen({ port: 3000 }, (err, address) => {
    if (err) {
        console.error(err);
        process.exit(1);
    }
    console.log(`Server listening at: ${address}`);
});

const ws = new WebSocket('ws://localhost:3000/chat?username=test');

Expected Behavior

No response

raythurnvoid commented 10 months ago

Mi Bad, i had to wrap the ws handler in an additional fastify.register. I was deceived by some outdated tutorial online.