moclojer
GitHub
  • README
  • First Steps
    • Overview
    • Installation
    • Your First Mock Server
    • Dynamic Responses
    • Multiple Endpoints
    • Real-World Example
  • Templates
    • Template System Overview
    • Template Variables
  • Advanced Features
    • WebSocket Support
    • External Bodies
    • Webhook Integration
    • Rate Limiting
    • Multi-Domain Support
  • Framework Integration
    • Using as a Library
  • Reference Documentation
    • Configuration Specification
    • FAQ
  • Community and Contribution
    • Documentation Refactor
    • Code of Conduct
  • Release Notes
    • Latest Changes
    • v0.4.0
    • v0.3.5
    • v0.3.4
    • v0.3.3.1
    • v0.3.3
    • v0.3.2
    • v0.3.1
    • v0.3.0
    • v0.2
    • v0.1
Powered by GitBook
On this page
  • WebSocket Properties
  • Using templates in responses
  • Testing WebSocket connections
  • WebSocket Echo Server

Was this helpful?

Edit on GitHub
  1. Advanced Features

WebSocket Support

Moclojer now supports WebSockets, allowing real-time bidirectional communication

It is possible to configure WebSocket endpoints for real-time bidirectional communication:

# WebSocket echo server
- websocket:
    path: /ws/echo
    on-connect:
      # Message sent when client connects
      response: '{"status": "connected", "message": "Welcome to WebSocket Echo!"}'
    on-message:
      # Simple echo for "ping" message
      - pattern: "ping"
        response: "pong"
      # Echo any JSON content with "echo" field
      - pattern: '{"echo": "{{json-params.echo}}"}'
        response: '{"echoed": "{{json-params.echo}}"}'

When receiving a connection at the /ws/echo endpoint, Moclojer will respond with {"status": "connected", "message": "Welcome to WebSocket Echo!"}.

When the client sends the message ping, the server will respond with pong.

If the client sends a JSON with the echo field, such as {"echo": "hello world"}, the server will respond with {"echoed": "hello world"}.

WebSocket Properties

  • path: The URL path for the WebSocket endpoint

  • on-connect: Configuration for the client connection event

    • response: Message sent to the client when it connects

  • on-message: List of message patterns and their respective responses

    • pattern: Message pattern to be matched

    • response: Response to be sent when the pattern is found

Using templates in responses

Just like HTTP endpoints, you can use templates in WebSocket responses:

- websocket:
    path: /ws/user/:username
    on-connect:
      response: '{"status": "connected", "message": "Welcome, {{path-params.username}}!"}'
    on-message:
      - pattern: '{"action": "get-profile"}'
        response: '{"user": "{{path-params.username}}", "profile": {"joined": "2025-03-20"}}'

You can access parameters from different sources:

  • path-params: URL path parameters (like :username in the example above)

  • query-params: URL query parameters

  • json-params: Fields in received JSON messages

Testing WebSocket connections

# Using websocat
websocat "ws://localhost:8000/ws/echo" --text

# Using wscat
wscat -c ws://localhost:8000/ws/echo

Example interaction:

Connected to ws://localhost:8000/ws/echo
< {"status": "connected", "message": "Welcome to WebSocket Echo!"}
> ping
< pong
> {"echo": "hello world"}
< {"echoed": "hello world"}

WebSocket support in Moclojer significantly simplifies the development and testing of applications that use real-time communication.

Swagger:

WebSocket Echo Server

GET /ws/echo

WebSocket endpoint that responds to "ping" messages with "pong" and echoes JSON messages.

{
  "status": "connected",
  "message": "Welcome to WebSocket Echo!"
}
PreviousTemplate VariablesNextExternal Bodies

Last updated 2 months ago

Was this helpful?

You can test WebSocket connections using tools like or :

websocat
wscat