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:

  1. Client initiates WebSocket handshake

  2. Server sends on-connect message (welcome/initialization)

  3. Client sends messages

  4. Server matches message patterns and responds

  5. 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

Field
Description

path

WebSocket endpoint path (e.g., /ws/chat)

Optional Fields

Field
Description
Default

on-connect

Message sent when client connects

(none)

on-message

List of message patterns and responses

[]

Message Pattern Matching

Each on-message entry supports:

Field
Description

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 wscat

JavaScript/Browser Test

Python Test

📝 Using Template Variables

WebSocket responses support all template variables:

Available Template Variables

Variable
Description
Example

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-connect message 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

Scenario
Configuration

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

  1. Open Chrome DevTools (F12)

  2. Go to Network tab

  3. Filter by WS (WebSocket)

  4. Click on WebSocket connection

  5. View Messages tab for full traffic

Common Issues

Issue
Solution

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

Last updated

Was this helpful?