Skip to main content

WSXServer

The main server class that manages WebSocket connections and request handling.

Constructor

Creates a new WSX server instance with the provided adapter. Parameters:
  • adapter - A WSXServerAdapter implementation (Express, Hono, or custom)
Example:

Methods

on()

Register handlers for WebSocket requests.
Parameters:
  • handler (optional) - Handler name to match against wx-send attribute
  • handlerFunction - Function to handle the request
Returns: WSXServer instance for chaining Examples:

onJson()

Register handlers for JSON message channels. Accepts either a specific channel or a catch-all handler.
Parameters:
  • channel (optional) - JSON channel name to handle
  • handler - Function invoked with WSXJSONMessage objects
Example:

onStream()

Register handlers for binary stream messages. Works with channel-specific or catch-all handlers.
Parameters:
  • channel (optional) - Stream channel name to handle
  • handler - Function invoked with WSXStreamMessage metadata and the binary payload
Example:

broadcast()

Send a message to all connected clients.
Parameters:
  • target - CSS selector for the target element
  • html - HTML content to insert
  • swap (optional) - Swap method (default: ‘innerHTML’)
Example:

broadcastJson()

Publish a JSON payload to every connected client.
Parameters:
  • channel - Logical channel name
  • data - JSON-serializable payload
  • options (optional) - Additional metadata or explicit identifier
Returns: Unique message identifier sent to clients Example:

broadcastStream()

Send a binary payload to every connected client.
Parameters:
  • channel - Stream channel name
  • payload - Binary data (ArrayBuffer or typed array)
  • options (optional) - Metadata or explicit stream identifier
Returns: Generated stream identifier Example:

sendToConnection()

Send a message to a specific connection.
Parameters:
  • connectionId - ID of the target connection
  • target - CSS selector for the target element
  • html - HTML content to insert
  • swap (optional) - Swap method (default: ‘innerHTML’)
Example:

sendJsonToConnection()

Send a JSON message to a specific connection.
Parameters:
  • connectionId - Target connection ID
  • channel - JSON channel name
  • data - JSON-serializable payload
  • options (optional) - Metadata or explicit message identifier
Returns: Message identifier, or undefined if the connection is not found Example:

sendStreamToConnection()

Send a binary stream frame to a specific connection.
Parameters:
  • connectionId - Target connection ID
  • channel - Stream channel name
  • payload - Binary payload (ArrayBuffer or typed array)
  • options (optional) - Metadata or explicit identifier
Returns: Stream identifier, or undefined if the connection is missing Example:

getConnections()

Get all active connections.
Returns: Array of active WSXConnection objects Example:

getConnectionCount()

Get the number of active connections.
Returns: Number of active connections Example:

removeConnection()

Remove a connection from the server.
Parameters:
  • connectionId - ID of the connection to remove
Example:

getApp()

Get the underlying framework app instance.
Returns: The framework-specific app instance (Express app, Hono app, etc.) Example:

WSXRequest

Interface representing an incoming WebSocket request.
Properties:
  • id - Unique identifier for the request
  • handler - Handler name specified in wx-send attribute
  • target - CSS selector from wx-target attribute
  • trigger - Event that triggered the request
  • data - Form data or data from wx-data attribute
  • swap - Swap specification from wx-swap attribute

WSXResponse

Interface representing a response to send back to the client.
Properties:
  • id - Must match the request ID
  • target - CSS selector for the element to update
  • html - HTML content to insert
  • swap - How to insert the content (innerHTML, outerHTML, etc.)
  • oob - Array of out-of-band updates for other elements

WSXOOBUpdate

Interface for out-of-band updates.
Properties:
  • target - CSS selector for the element to update
  • html - HTML content to insert
  • swap - How to insert the content

WSXConnection

Interface representing a WebSocket connection.
Properties:
  • id - Unique identifier for the connection
  • sessionData - Object for storing session-specific data
  • send() - Method to send data to the client
  • close() - Method to close the connection

WSXHandler

Type definition for handler functions.
Parameters:
  • request - The incoming request
  • connection - The connection that sent the request
Returns: Promise that resolves to:
  • WSXResponse - Single response
  • WSXResponse[] - Array of responses
  • void - No response (useful for side effects only)

WSXBinaryData

Union type representing allowed binary payloads.

WSXJSONMessage

Structure passed to JSON handlers and received on the client.

WSXJSONHandler

Type definition for JSON message handlers.

WSXJSONSendOptions

Optional options when sending JSON messages.

WSXStreamMessage

Metadata describing a binary stream frame.

WSXStreamHandler

Type definition for stream handlers.

WSXStreamSendOptions

Optional options when sending binary stream frames.

WSXServerAdapter

Interface that framework adapters must implement.
Methods:
  • setupWebSocket() - Set up WebSocket handling
  • onConnection() - Optional connection handler
  • onDisconnection() - Optional disconnection handler
  • getApp() - Return the framework app instance

Error Handling

WSX provides built-in error handling, but you can customize it:

Best Practices

Handler Organization

Connection Management

Broadcasting Patterns

Next Steps

Client API

Learn about the client-side API

Type Definitions

Complete TypeScript type definitions

Examples

See real-world usage examples

Advanced Usage

Advanced patterns and optimization