Skip to content

[bug] Fastify-websocket does not support route prefixes #298

Open
@you-hengh

Description

@you-hengh

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.27.0

Plugin version

10.0.1

Node.js version

20.13.0

Operating system

Windows

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

Windows 11 22631.3593

Description

I noticed #97 and #98 , but they didn't solve the problem I encountered!

I used the routing module in the server, imported routes from other files, and used route prefixes

// server.js
import fastify from 'fastify';

const server = fastify({
  logger: {
    transport: {
      target: 'pino-pretty',
      options: {
        translateTime: 'SYS:HH:MM:ss',
        ignore: 'pid,hostname',
      },
    },
  },
});


server.register(import('@fastify/websocket'));


server.register(import('./routes/index.js'), { prefix: '/restaurant' });


server.listen({ port: 3000, host: '127.0.0.1' }, (err, address) => {
  if (err) {
    server.log.error(err, address);
    process.exit(1);
  }
});

Contents in './routes/index.js':

// ./routes/index.js
import fastifyPlugin from 'fastify-plugin';

const router = async fastify => {
  fastify.get('/', { websocket: true }, (socket, req) => {
    socket.on('message', message => {
      socket.send('hi from server');
    });
  });
};

export default fastifyPlugin(router);

I used the link ws://localhost:3000/restaurant in the test software to not link to the server normally,

2024-06-04_02-25-52

But successfully linked to the server with ws://localhost:3000/

2024-06-04_02-28-05

Link to code that reproduces the bug

No response

Expected Behavior

Make the link adhere to the route prefix, access the websocket through ws://localhost:3000/restaurant

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions