Client API Reference
The WSX client provides a JavaScript API for interacting with WSX servers, sending requests, and handling responses.WSX Client Class
Constructor
Parameters
options
(Object): Configuration optionsurl
(string): WebSocket server URLautoConnect
(boolean): Auto-connect on instantiation (default:true
)reconnect
(boolean): Auto-reconnect on connection loss (default:true
)reconnectInterval
(number): Reconnection interval in ms (default:3000
)maxReconnectAttempts
(number): Maximum reconnection attempts (default:10
)debug
(boolean): Enable debug logging (default:false
)
Example
Connection Methods
connect()
Establishes a WebSocket connection to the server.Returns
Promise<void>
: Resolves when connection is established
Example
disconnect()
Closes the WebSocket connection.Example
isConnected()
Checks if the client is currently connected.Returns
boolean
:true
if connected,false
otherwise
Example
Request Methods
send()
Sends a request to the server.Parameters
handler
(string): Handler name on the servertarget
(string): CSS selector for the target elementdata
(Object): Data to send with the request (optional)options
(Object): Request options (optional)swap
(string): How to swap the response contenttrigger
(string): Trigger informationtimeout
(number): Request timeout in ms
Returns
Promise<WSXResponse>
: Promise that resolves with the server response
Example
sendForm()
Sends form data to the server.Parameters
handler
(string): Handler name on the servertarget
(string): CSS selector for the target elementformElement
(HTMLFormElement): Form element to serializeoptions
(Object): Request options (optional)
Returns
Promise<WSXResponse>
: Promise that resolves with the server response
Example
sendFile()
Sends a file to the server.Parameters
handler
(string): Handler name on the servertarget
(string): CSS selector for the target elementfile
(File): File to uploadoptions
(Object): Request options (optional)onProgress
(Function): Progress callback function
Returns
Promise<WSXResponse>
: Promise that resolves with the server response
Example
Event Handling
on()
Registers an event listener.Parameters
event
(string): Event namecallback
(Function): Event handler function
Events
connect
: Fired when connection is establisheddisconnect
: Fired when connection is closedmessage
: Fired when a message is receivederror
: Fired when an error occursreconnect
: Fired when reconnection startsreconnected
: Fired when reconnection succeeds
Example
off()
Removes an event listener.Parameters
event
(string): Event namecallback
(Function): Event handler function to remove
Example
once()
Registers a one-time event listener.Parameters
event
(string): Event namecallback
(Function): Event handler function
Example
DOM Integration
processResponse()
Processes a server response and updates the DOM.Parameters
response
(WSXResponse): Server response object
Example
swapContent()
Swaps content in a target element.Parameters
target
(string|HTMLElement): Target element or selectorhtml
(string): HTML content to swapswapType
(string): How to swap the content
Swap Types
innerHTML
: Replace element’s inner HTMLouterHTML
: Replace the entire elementbeforebegin
: Insert before the elementafterbegin
: Insert as first childbeforeend
: Insert as last childafterend
: Insert after the element
Example
Utility Methods
generateId()
Generates a unique request ID.Returns
string
: Unique identifier
Example
serializeForm()
Serializes a form element to an object.Parameters
formElement
(HTMLFormElement): Form element to serialize
Returns
Object
: Serialized form data
Example
parseSelector()
Parses a CSS selector and returns element information.Parameters
selector
(string): CSS selector
Returns
Object
: Parsed selector information
Example
Configuration Methods
setConfig()
Updates client configuration.Parameters
options
(Object): Configuration options to update
Example
getConfig()
Gets current client configuration.Returns
Object
: Current configuration
Example
Request Queue Methods
getQueueSize()
Gets the current request queue size.Returns
number
: Number of queued requests
Example
clearQueue()
Clears the request queue.Example
Static Methods
WSX.create()
Creates a new WSX instance with default configuration.Parameters
url
(string): WebSocket server URLoptions
(Object): Configuration options (optional)
Returns
WSX
: New WSX instance
Example
WSX.version
Gets the WSX client version.Returns
string
: Version string
Example
Error Handling
WSXError
Base error class for WSX-related errors.Properties
message
(string): Error messagecode
(string): Error codedetails
(Object): Additional error details
Example
ConnectionError
Error thrown when connection fails.TimeoutError
Error thrown when request times out.ValidationError
Error thrown when request validation fails.HTML Attributes
wx-config
Configures WSX for a page or element.wx-send
Specifies the handler to call when triggered.wx-target
Specifies the target element for the response.wx-trigger
Specifies when to trigger the request.wx-swap
Specifies how to swap the response content.wx-include
Specifies what data to include with the request.wx-confirm
Shows a confirmation dialog before sending the request.wx-disable
Disables the element while the request is in progress.wx-indicator
Shows a loading indicator while the request is in progress.Global Functions
window.wsx
Global WSX instance created automatically when WSX is initialized with HTML attributes.Example
wx()
Shorthand function for accessing the global WSX instance.Best Practices
- Error Handling: Always wrap WSX calls in try-catch blocks
- Event Cleanup: Remove event listeners when no longer needed
- Connection Management: Handle connection state changes appropriately
- Request Queuing: Monitor queue size to prevent memory issues
- Performance: Use appropriate swap types for optimal DOM updates
- Security: Validate and sanitize all data before sending
- Accessibility: Ensure dynamic content updates are accessible
Browser Compatibility
WSX client supports:- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 79+
- WebSocket
- Promise
- fetch API
Next Steps
- Learn about Type Definitions for TypeScript support
- Explore Performance Optimization for client-side optimization
- Understand Security Best Practices for secure client implementation