WebSocket support
Moclojer now supports WebSockets, allowing real-time bidirectional communication
It is possible to configure WebSocket endpoints for real-time bidirectional communication:
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 endpointon-connect
: Configuration for the client connection eventresponse
: Message sent to the client when it connects
on-message
: List of message patterns and their respective responsespattern
: Message pattern to be matchedresponse
: Response to be sent when the pattern is found
Using templates in responses
Just like HTTP endpoints, you can use templates in WebSocket responses:
You can access parameters from different sources:
path-params
: URL path parameters (like:username
in the example above)query-params
: URL query parametersjson-params
: Fields in received JSON messages
Testing WebSocket connections
You can test WebSocket connections using tools like websocat or wscat:
Example interaction:
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.
Last updated
Was this helpful?