Skip to main content

Client Events

WSX provides a comprehensive event system for handling WebSocket responses, connection states, and client-side interactions. These events allow you to create rich, responsive user experiences.

Connection Events

wsx:connected

Fired when WebSocket connection is established:

wsx:disconnected

Fired when WebSocket connection is lost:

wsx:error

Fired when WebSocket errors occur:

Request Events

wsx:beforeRequest

Fired before sending a request to the server:

wsx:afterRequest

Fired after receiving a response from the server:

wsx:requestError

Fired when a request fails:

Data Channel Events

wsx:json

Fired whenever a JSON message arrives from the server or another client:
Pair DOM listeners with programmatic handlers registered via wsx.onJson() for fine-grained control.

wsx:stream

Fired for each binary stream frame that lands on the client:
Use wsx.onStream() when you prefer callback registration and the wsx:stream event when you need declarative DOM hooks.

Swap Events

wsx:beforeSwap

Fired before content is swapped:

wsx:afterSwap

Fired after content is swapped:

wsx:swapError

Fired when content swap fails:

Element-Specific Events

Element Event Listeners

Add event listeners to specific elements:

Form Events

Handle form-specific events:

Real-World Event Handling

Loading States

Error Handling

Connection Management

Animation and Transitions

Smooth Transitions

Progress Indicators

Advanced Event Patterns

Event Delegation

Custom Event Dispatch

Event Filtering

Event Utilities

Event Logger

Event Metrics

Testing Events

Event Testing

Mock Events

Best Practices

  1. Use Specific Event Listeners: Target specific elements when possible
  2. Clean Up Event Listeners: Remove listeners when elements are removed
  3. Handle Errors Gracefully: Provide user-friendly error messages
  4. Debounce Heavy Operations: Avoid too frequent event handling
  5. Use Event Delegation: Handle events for dynamically added content
  6. Test Event Handlers: Ensure event handling works correctly
  7. Monitor Performance: Track event frequency and performance impact

Common Patterns

Notification System

Auto-Save Indicator

Next Steps