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

[Feature] Global WebSocket Hub with Channels & Advanced Broadcasting #138

Open
diyor28 opened this issue Feb 18, 2025 · 0 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@diyor28
Copy link
Contributor

diyor28 commented Feb 18, 2025

Problem

The current WebSocket setup is limited to chat components, preventing real-time functionality across the application. Additionally, the existing hub lacks flexible broadcasting and per-connection context handling.


Objectives

1. Global WebSocket Integration

  • Move ws-connect in HTMX as close to <body> as possible to initialize WebSocket early and enable it app-wide.

2. Global WebSocket Hub

  • Refactor the WebSocket hub into a globally accessible component for all parts of the application.

3. Channel-Based Subscriptions

  • Implement dynamic channel support in the hub:
    • Connections can subscribe to multiple channels simultaneously.
  • Channels should be flexible and arbitrary, not limited to pre-defined types.

4. Per-Connection Context

  • Each connection should hold contextual data (e.g., User info, Session details) in-memory.
  • Context should be available when broadcasting.

Broadcasting API – Required Methods

Method Description
BroadcastToUser(userID string, message) Send a message to a single user.
BroadcastToChannel(channel string, message) Send a message to all connections in a specific channel.
BroadcastToAll(message) Send a message to all active connections.

Connection & Channel Management – Required Methods

The hub should provide access to active connections:

Method Description
GetConnectionsInChannel(channel string) Iterate over all connections in a specific channel.
GetAllConnections() Iterate over all active connections globally.

Each connection object should expose:

  • User information (e.g., UserID, SessionID)
  • Subscribed channels (e.g., []string)
  • SendMessage(message) – Helper method to send a message to this connection.

Expected Outcome

  • WebSocket functionality is available globally.
  • The hub can:
    • Broadcast to a single user.
    • Broadcast to a channel (e.g., company/29).
    • Broadcast to all connections.
  • Developers can:
    • Access active connections by channel.
    • Iterate over all active connections.
  • Each connection carries user/session context for smarter message handling.

Suggested Implementation Plan

  1. Move ws-connect (HTMX) near <body>.
  2. Implement a global WebSocket hub.
  3. Introduce channels with dynamic string names.
  4. Add per-connection context (user, session).
  5. Build broadcast methods (user, channel, all).
  6. Expose connection/channel iterators.

Additional Considerations

  • Ensure thread-safety when managing connections and channels.
  • Design hub with room for future scaling (e.g., cross-server pub/sub if needed later).
@diyor28 diyor28 added the enhancement New feature or request label Feb 18, 2025
@diyor28 diyor28 added this to SDK core Feb 18, 2025
@diyor28 diyor28 self-assigned this Feb 23, 2025
@thelissimus-work thelissimus-work self-assigned this Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants