Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@productdevbook
Copy link
Member

@productdevbook productdevbook commented Jul 7, 2025

Summary

cc: #1088
cc: #1138

  • Added defineWebSocketRoute utility function to simplify WebSocket route definitions
  • Added WebSocketRouteDefinition interface for type-safe WebSocket route configuration
  • Exported new utilities from main index.ts

Description

This PR introduces a new defineWebSocketRoute function that provides a consistent API pattern with defineRoute for defining WebSocket endpoints. This makes it easier to create WebSocket routes as plugins that can be registered with app.register().

Example usage:

import { defineWebSocketRoute } from "h3";

export const webhook = defineWebSocketRoute({
  route: "/api/webhook",
  websocket: {
    open: (peer) => {
      console.log("WebSocket connected:", peer.id);
      peer.send("Welcome\!");
    },
    message: (peer, message) => {
      console.log("Incoming message:", message);
      peer.send(`Received: ${message}`);
    },
    close: (peer) => {
      console.log("WebSocket closed:", peer.id);
    },
  }
});

// Register with app
app.register(webhook);

Test plan

  • Test WebSocket route registration with app.register()
  • Verify WebSocket upgrade works correctly
  • Test with different route patterns
  • Ensure backwards compatibility with existing WebSocket handler methods

@productdevbook productdevbook requested a review from pi0 as a code owner July 7, 2025 08:43
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 7, 2025

Deploying h3dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: fd6f13d
Status: ✅  Deploy successful!
Preview URL: https://71a3c637.h3dev.pages.dev
Branch Preview URL: https://feat-definewebsocketroute.h3dev.pages.dev

View logs

@productdevbook productdevbook changed the title feat: add defineWebSocketRoute utility feat: experimental defineWebSocketRoute Jul 7, 2025
@productdevbook productdevbook mentioned this pull request Jul 7, 2025
1 task
@codecov
Copy link

codecov bot commented Jul 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@productdevbook productdevbook marked this pull request as draft July 7, 2025 09:40
/**
* Additional route metadata
*/
meta?: Record<string, unknown>;
Copy link
Member

@pi0 pi0 Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Middleware and meta are not supported. Types should be removed (at least untill they are supported by defineWebSocket

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take the comment line and leave a TODO ?

/**
* HTTP method for the route (typically 'GET' for WebSocket upgrades)
*/
method?: HTTPMethod;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocket handlers should not accept a method.

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

Successfully merging this pull request may close these issues.

3 participants