Giter Club home page Giter Club logo

Comments (9)

nksaraf avatar nksaraf commented on July 30, 2024 6

I am working with the nitro team on this. We already enabled websocket support for the dev server using @vinxi/listhen, a temporary fork of listhen that enables websocket support until unjs/listhen#128 lands.

We will then try to land APIs for h3 and nitro that make it easy to write cross-platform websocket servers

from vinxi.

shiro avatar shiro commented on July 30, 2024

Now that the referenced PR is closed, is there any place to track the progress on WS support, h3/nitro API, etc.?

from vinxi.

nksaraf avatar nksaraf commented on July 30, 2024

So looks like unjs has introduced crossws package. This will be integrated in h3/nitro soon. I don't think there is a tracking issue, but we should get this soon. @pi0 i don't know if we have a timeline yet right

from vinxi.

bondehagen avatar bondehagen commented on July 30, 2024

I guess the important part that is missing, is the hook to listen for the upgrade event unjs/nitro#2041
Hopefully @pi0 will take a look at it soon.

from vinxi.

nksaraf avatar nksaraf commented on July 30, 2024

released in 0.3.6

from vinxi.

shiro avatar shiro commented on July 30, 2024

Thanks a lot! Is there a tracking issue for bringing this into start?

from vinxi.

nksaraf avatar nksaraf commented on July 30, 2024

Its immediately available in start. Set server.experimental.websocket: true in config file.

You can then add a new router to your vinxi app, like this:

import { defineConfig } from "@solidjs/start/config";

const app = defineConfig({
  server: {
    experimental: {
      websocket: true,
    },
  },
});

app.addRouter({
  name: "websocket",
  type: "http",
  handler: "./src/websocket.ts",
  target: "server",
  base: "/_ws",
});

export default app;

src/websocket.ts

import { defineWebSocket, eventHandler } from "vinxi/http";

export default eventHandler({
	handler: () => {},
	websocket: defineWebSocket({
		async open(event) {
			console.log("WebSocket opened");
		},
		async message(peer, event) {
			console.log("WebSocket message", event);
			peer.send("YOOO");
		},
		async close(event) {
			console.log("WebSocket closed 3");
		},
	}),
});

To connect to the websocket try,

const ws = new WebSocket('ws://localhost:3000/_ws')

from vinxi.

frenzzy avatar frenzzy commented on July 30, 2024

Is there a way to import WSRequest, Peer, Message etc. types from vinxi?

defineWebSocket({
  upgrade: (req: WSRequest) => MaybePromise<void | { headers?: HeadersInit }>;
  message: (peer: Peer, message: Message) => MaybePromise<void>;
  open: (peer: Peer) => MaybePromise<void>;
  close: (peer: Peer, details: { code?: number; reason?: string }) => MaybePromise<void>;
  error: (peer: Peer, error: WSError) => MaybePromise<void>;
})

from vinxi.

frenzzy avatar frenzzy commented on July 30, 2024

Thanks a lot! Is there a tracking issue for bringing this into start?

Maybe it should look something like this: solidjs/solid-start#1384

from vinxi.

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.