Handlers
Handlers are the core mechanism for processing WebSocket requests in WSX. They define how your server responds to different types of client interactions.Handler Basics
Handler Function Signature
All handlers follow the same signature:Registering Handlers
Register handlers using theon
method:
Request Object
WSXRequest Properties
Accessing Request Data
Response Object
WSXResponse Properties
Basic Response
Handler Types
Named Handlers
Handle specific actions by name:Event-Based Handlers
Handle by trigger type:Catch-All Handler
Handle all unmatched requests:Advanced Handler Patterns
Conditional Logic
Async Operations
Multiple Responses
Return arrays for multiple updates:Handler Chaining
Middleware Pattern
Validation
Error Handling
Try-Catch Pattern
Graceful Degradation
Best Practices
- Always Return Responses: Handlers should return a response object
- Handle Errors: Use try-catch blocks for async operations
- Validate Input: Check request data before processing
- Use Session Data: Store connection-specific state in
sessionData
- Keep Handlers Focused: Each handler should handle one specific action
- Log Appropriately: Log errors and important events
Next Steps
- Learn about Triggers for client-side events
- Explore Out-of-Band Updates for multiple element updates
- Understand Swaps for content replacement strategies