Coder Social home page Coder Social logo

Websocket definition in groups about elysia HOT 9 CLOSED

elysiajs avatar elysiajs commented on May 16, 2024
Websocket definition in groups

from elysia.

Comments (9)

Blechlawine avatar Blechlawine commented on May 16, 2024 1

I think I'm having the same issue. When I register a websocket handler in a group, like this:

import Elysia from "elysia";

new Elysia()
    .group("/ws", (a) =>
        a.ws("/", {
            open() {
                console.log("Hello");
            },
        }),
    )
    .listen(3000);

When trying to connect to it, i always get an error code 400 with this message:
Expected a websocket connection

I think it might have something to do with server when upgrading the request. Maybe server ist undefined | null, or another falsy value, thus the if upgrade fails, and this error is returned? (relevant code: https://github.com/elysiajs/elysia/blob/4a1134b082b726eb67b0581565628a1d697a0c18/src/index.ts#L2401C5-L2402C45)

I`m on elysia version 0.7.15

from elysia.

SaltyAom avatar SaltyAom commented on May 16, 2024

Thanks for the report, from a technical perspective, it's expected.

However, I think your approach should work by default too, and I update Elysia 0.3.2 to work with the first snippet.

Should be fixed with #f313078, please update to 0.3.2 to verify if your first snippet work.

from elysia.

ntzrbtr avatar ntzrbtr commented on May 16, 2024

I worked on my project further last night and somehow the websocket connection could not be established when it was defined inside the group. So now I moved it outside and it is working.

So from

import { Elysia, ws } from "elysia";

const app = new Elysia()
	.use(ws())
	.get("/", () => "Welcome to Elysia!")
	.group("/group", (app) => app.get("/", myHandler).ws("/websocket", {}))
	.listen(3000);

I moved to

import { Elysia, ws } from "elysia";

const app = new Elysia()
	.use(ws())
	.get("/", () => "Welcome to Elysia!")
	.group("/group", (app) => app.get("/", myHandler))
	.ws("/group/websocket", {})
	.listen(3000);

from elysia.

SaltyAom avatar SaltyAom commented on May 16, 2024

Yes, that's expected. And I'm trying to make ws inside group work too.

With the patch of Elysia 0.3.2, this code should also work:

import { Elysia, ws } from "elysia";

const app = new Elysia()
	.use(ws())
	.get("/", () => "Welcome to Elysia!")
	.group("/group", (app) => app.get("/", myHandler).ws("/websocket", {}))
	.listen(3000);

Can you verify that the code above works on your end too?

from elysia.

ntzrbtr avatar ntzrbtr commented on May 16, 2024

Sorry, I wasn't working on my project for some days.

Should I test this again with v0.3.2 or directly with the latest v0.4.5?

from elysia.

SaltyAom avatar SaltyAom commented on May 16, 2024

0.4.5 should work

from elysia.

ntzrbtr avatar ntzrbtr commented on May 16, 2024

OK, I tested this extensively and when defining the websocket route inside a group, the connection can't be upgraded. I tried to debug this, but I can't see a reason for the upgrade failure, as Bun just returns a boolean when calling server.upgrade().

from elysia.

vandelli avatar vandelli commented on May 16, 2024

+1 same issue here with version 0.7.15
Unable to connect to ws after using it under group.

from elysia.

SaltyAom avatar SaltyAom commented on May 16, 2024

Fix with eabe264, available on 0.7.27.

Feel free to reopen if the issue still persists.

from elysia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.