Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
you-hengh opened this issue Jun 3, 2024 · 0 comments
Open
2 tasks done

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

you-hengh opened this issue Jun 3, 2024 · 0 comments

Comments

@you-hengh
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant