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

WebSocket Helper not working #2645

Open
mathysth opened this issue May 8, 2024 · 5 comments
Open

WebSocket Helper not working #2645

mathysth opened this issue May 8, 2024 · 5 comments
Labels

Comments

@mathysth
Copy link

mathysth commented May 8, 2024

What version of Hono are you using?

4.1.1

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

I'm using Hono websocket helper but it return me and error when I try to connect to it
image
I cheked if my setup was good and for me it was
image
Here how I setup the ws to hono
image

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@mathysth mathysth added the bug label May 8, 2024
@nakasyou
Copy link
Contributor

nakasyou commented May 8, 2024

Hi @mathysth
Could you show me that unhidden fetch function in 2nd screenshot?
I think that problem is in it.

@mathysth
Copy link
Author

mathysth commented May 8, 2024

Hey @nakasyou here the full code that you ask

boostrapApp().then((httpConfig) => {
  const logger = IocContainer.container.get(LoggerService);
  const controllerRoot = IocContainer.container.get(ControllerRoot);
  const socket = IocContainer.container.get(SocketProvider);
  try {
    serve({
      async fetch(req, server) {
        const serverConfig = await httpConfig?.fetch(req, { ip: server.requestIP(req) });
        return serverConfig;
      },
      websocket,
      port: httpConfig?.port,
    });
    // Initialize the socket
    socket.initSocket();

    // Setup all controllers
    controllerRoot.setup();
    logger.pino.info(`Hono 🥟 Server Listening on port ${httpConfig?.port}`);
  } catch (e) {
    logger.pino.error(`An error occurred during server initialization, ${e}`);
  }
});

@mathysth
Copy link
Author

mathysth commented May 8, 2024

@nakasyou I found the problem but I'm still searching for the anwser.
according to hono doc you can retrieve user ip like the following :

const app = new Hono()

serve({
  fetch: (req, server) => {
    return app.fetch(req, { ip: server.requestIP(req) })
  },
  websocket,
  port: 3008,
});

But when you bind the ip as an env I think the server is not initialize... and then the websocket logic doesn't work anymore.
If I remove { ip: server.requestIP(req) } it's working for me

@nakasyou
Copy link
Contributor

nakasyou commented May 8, 2024

@mathysth, this may also be a problem with Hono.

As a workaround, you can use that:

await httpConfig?.fetch(req, { upgrade: server.upgrade })

or

await httpConfig?.fetch(req, server)

@mathysth
Copy link
Author

mathysth commented May 8, 2024

@nakasyou I did this workaround, it's working, hope hono could fix this somedays. Thanks again
Also maybe this section need some edit

serve({
      async fetch(req, server) {
        const untypedServer = server as unknown as any
        untypedServer.ip = server.requestIP(req)
        const serverConfig = await httpConfig?.fetch(req, untypedServer);
        return serverConfig;
      },
      websocket,
      port: httpConfig?.port,
    });

mathysth added a commit to geniqks/cosmoosjs_angular_ecommerce that referenced this issue May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants