Overview
This quick start guide will get you up and running with WSX in under 5 minutes. We’ll build a simple real-time counter that updates across all connected clients.Prerequisites
- Node.js 18 or later
- npm or yarn
Step 1: Install WSX
Choose your framework and install the corresponding packages:Step 2: Create Your Server
Step 3: Add the Client Library
Copy the WSX client library to your public directory:Step 4: Run Your Application
Step 5: Test It Out
- Open your browser to
http://localhost:3000
(orhttp://localhost:8787
for Hono) - Click the “Increment” button
- Open another browser tab/window to the same URL
- Click the button in either tab - both will update in real-time!
What Just Happened?
Let’s break down what we built:HTML Attributes
wx-config
: Configures the WebSocket connectionwx-send
: Defines the handler name to call on the serverwx-target
: Specifies which element to update with the response
Server Handler
Thewsx.on("increment", ...)
handler:
- Increments the counter
- Broadcasts the update to all connected clients
- Returns a response for the triggering client
Real-time Updates
When you click the button:- WSX sends a WebSocket message to the server
- The server processes the request and updates the counter
- The server broadcasts the new count to all connected clients
- All browser windows update simultaneously
Next Steps
Now that you have WSX running, explore these features:Advanced Triggers
Learn about throttling, debouncing, and conditional triggers
Out-of-Band Updates
Update multiple DOM elements from a single response
Form Handling
Build real-time forms with validation and submission
Broadcasting
Send targeted updates to specific clients or groups
Common Issues
WebSocket connection fails
WebSocket connection fails
Make sure your WebSocket URL in
wx-config
matches your server
configuration. Check the browser console for connection errors.Button clicks don't work
Button clicks don't work
Updates aren't real-time
Updates aren't real-time
Verify that your server handler is calling
wsx.broadcast()
to update all
connected clients, not just returning a response.