fastify / fastify-websocket

basic websocket support for fastify
MIT License
400 stars 74 forks source link

Unable to connect to WS, status remains connecting on Postman #250

Closed gchicoye closed 1 year ago

gchicoye commented 1 year ago

Prerequisites

Fastify version

4.15.0

Plugin version

7.2.0

Node.js version

18.9.1

Operating system

macOS

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

13.2.1

Description

Hi, I create a Fastify Websocket server following this tutorial. https://blog.logrocket.com/using-websockets-with-fastify/. However, although I get Connection established through the following code, I cannot succeed connecting to the WS through Postman, status remains connecting. https://postimg.cc/Wqh980p2

Steps to Reproduce

The source code is as follows.

const fastify = require('fastify')();
fastify.register(require('@fastify/websocket'));

fastify.get('/hello', (request, reply) => {
    reply.send({
        message: 'Hello Fastify'
    });
});
fastify.get('/hello-ws', { websocket: true }, (connection, req) => {
  console.log('Connection established');
  connection.socket.on('message', message => {
      connection.socket.send('Hello Fastify WebSockets');
  });
});

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

When I ping the server, I get

npm run start

> ws-test@1.0.0 start
> node main.js

Server listening at: http://[::1]:3000
Connection established

Expected Behavior

I expect to have a socket connected

gchicoye commented 1 year ago

Hi, Any idea on this? Any logs to look at to provide you additional information?

climba03003 commented 1 year ago

Either await fastify.register or wrap your route with plugin. Please read the document about the proper usage.

gchicoye commented 1 year ago

Hi, thanks for help and sorry about that!