Real-time features such as chat, notifications or live order status are hard to test against a server you don't control. A WebSocket endpoint in Mockhop gives your client a socket that greets it, answers its messages the way you define and pushes messages when you decide.
How it works
Add a WebSocket endpoint to a project. Its address looks like this:
$ WS=wss://acme.mockhop.cloud
$ wscat -c $WS/ws/payments/orders
acme is your organization, payments the project's base path and /orders the endpoint's path. The example uses wscat, but any WebSocket client works.
- Welcome message: sent as soon as a client connects.
- Responses: each one has a match pattern, a body and an optional delay. When the client sends a message, Mockhop answers with the first response whose pattern matches:
- the message is exactly the pattern text, or
- the pattern is a JSON object, and every field in it appears in the client's message with the same value. A nested object in the pattern must match the client's value exactly.
- Default response: a response without a pattern answers any message. Responses are checked in order, so put it last.
- Broadcast: from the dashboard, send a message to every client connected to the endpoint, for example to simulate a server-side event.
Example
Welcome message {"type":"welcome"}. A response with pattern {"type":"sub"} and body {"type":"subscribed"}:
< {"type":"welcome"}
> {"type":"sub","ch":"orders"}
< {"type":"subscribed"}
The client's message has more fields than the pattern; it still matches because type is the same.
Good to know
- Message log: the endpoint records incoming and outgoing messages.
- Counting: opening a connection counts as one request. Messages over the connection don't count, in either direction, including the welcome message and broadcasts.
- Plans: WebSocket mocks are part of the Pro, Team and Business plans.
New to Mockhop? Start with the quickstart. Plans: pricing.