WebSocket Support
Configure WebSocket endpoints in moclojer for real-time bidirectional communication. Perfect for mocking chat applications, live notifications, and collaborative features.
moclojer supports WebSocket endpoints, enabling real-time bidirectional communication between client and server. This is essential for testing chat applications, live notifications, collaborative editing, and any real-time features without requiring a live WebSocket server.
🎯 How WebSockets Work in moclojer
WebSocket connections maintain a persistent bidirectional channel:
Client initiates WebSocket handshake
Server sends
on-connectmessage (welcome/initialization)Client sends messages
Server matches message patterns and responds
Connection persists until explicitly closed
sequenceDiagram
participant Client
participant moclojer
Client->>moclojer: WebSocket Handshake (HTTP Upgrade)
moclojer->>Client: 101 Switching Protocols
Note over moclojer,Client: WebSocket Connection Established
moclojer->>Client: on-connect message
loop Message Exchange
Client->>moclojer: Send message
Note over moclojer: Pattern matching
moclojer->>Client: Response (if pattern matches)
end
Client->>moclojer: Close connection
moclojer->>Client: Connection closed📝 Basic Configuration
Simple Echo Server
Testing:
Interaction:
🔧 WebSocket Configuration
Required Fields
path
WebSocket endpoint path (e.g., /ws/chat)
Optional Fields
on-connect
Message sent when client connects
(none)
on-message
List of message patterns and responses
[]
Message Pattern Matching
Each on-message entry supports:
pattern
Message pattern to match (string or JSON with templates)
response
Response to send when pattern matches
💬 Chat Application
Real-time chat with username support:
Testing the chat:
🔔 Real-Time Notifications
Live notification feed:
📊 Live Dashboard Updates
Real-time metrics and stats:
🎮 Game Server
Multiplayer game state:
🧪 Testing WebSockets
Using websocat (Recommended)
Using wscat
JavaScript/Browser Test
Python Test
📝 Using Template Variables
WebSocket responses support all template variables:
Available Template Variables
path-params.*
URL path parameters
{{path-params.username}}
query-params.*
Query string parameters
{{query-params.token}}
json-params.*
Fields from JSON messages
{{json-params.text}}
header-params.*
WebSocket headers
{{header-params.Authorization}}
{{now}}
Current timestamp
2025-01-01T12:00:00Z
✅ Best Practices
Do:
✅ Send
on-connectmessage for initialization✅ Use JSON for structured messages
✅ Include timestamps in responses for debugging
✅ Use path parameters for resource identification (
/ws/chat/:roomId)✅ Test with multiple concurrent connections
✅ Include message types for client-side routing
Don't:
❌ Hardcode dynamic data (use template variables)
❌ Forget to handle connection/disconnection events
❌ Send large binary data (WebSockets are for text/small payloads)
❌ Use complex regex in patterns (keep it simple)
🔧 Advanced Patterns
Multi-Pattern Matching
Conditional Responses
While WebSocket doesn't support if conditions like webhooks, you can simulate with different patterns:
🚨 Important Notes
Pattern Matching Order: Patterns are evaluated in order. First match wins. Place more specific patterns before generic ones.
No Broadcasting: moclojer WebSocket responses go only to the connection that sent the message. For multi-user broadcasting, use an actual WebSocket server.
Connection State: moclojer doesn't maintain cross-connection state. Each connection is independent.
Binary Messages: Currently, moclojer WebSocket support is optimized for text/JSON messages.
📊 Use Cases
Chat application
Room-based paths, join/leave/message patterns
Live notifications
User-specific paths, subscription patterns
Real-time dashboard
Metric updates, refresh patterns
Collaborative editing
Document paths, change events
Game servers
Game state, player actions
Live feeds
Social media updates, activity streams
🔍 Debugging WebSockets
Enable Verbose Logging
Monitor Traffic with Browser DevTools
Open Chrome DevTools (F12)
Go to Network tab
Filter by WS (WebSocket)
Click on WebSocket connection
View Messages tab for full traffic
Common Issues
Connection refused
Ensure moclojer is running and path is correct
Pattern not matching
Check JSON format and template syntax
No response received
Verify pattern matches exactly
Connection closes immediately
Check on-connect message format
📚 See Also
Template Variables - All available template variables
Path Parameters - Using path params in WebSocket paths
Real-World Example - Complete examples
Multi-Domain Support - WebSockets with different hosts
Last updated
Was this helpful?