WebSockets
Create mock WebSocket endpoints for real-time communication.
What is WebSocket Support?
Mockario now supports WebSocket endpoints, allowing you to create mock servers that simulate real-time bidirectional communication. This is perfect for testing chat applications, live notifications, and other real-time features.
Creating WebSocket Endpoints
You can create WebSocket endpoints through the web interface:
- Navigate to the WebSocket section in the sidebar
- Click “Add WebSocket Endpoint”
- Configure the path (e.g., /ws/chat)
- Set the event type and response
- Click “Create”
Or via the API:
JSON
POST /api/ws-endpoints
{
"path": "/ws/chat",
"eventType": "message",
"response": {
"message": "Welcome to the chat!",
"user": "{{faker.person.fullName}}"
},
"delay": 0
}Connecting to WebSocket
In your client application, connect using:
JavaScript
// JavaScript client example
const ws = new WebSocket('ws://localhost:3001/ws/chat');
ws.onopen = () => {
console.log('Connected to WebSocket');
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received:', data);
};
ws.send(JSON.stringify({ message: 'Hello!' }));Event Types
WebSocket endpoints can handle different event types:
- message - message - Handles incoming messages
- connection - connection - Triggered on new connections
- disconnect - disconnect - Triggered when client disconnects
- error - error - Triggered on errors
Faker Templates
You can use Faker templates in your responses to generate realistic data. Use the {{faker.module.method}} syntax.
Person
| Template | Example Output |
|---|---|
| {{faker.person.fullName}} | John Doe |
| {{faker.person.firstName}} | Jane |
| {{faker.person.lastName}} | Smith |
| {{faker.person.email}} | jane.smith@example.com |
| {{faker.person.jobTitle}} | Software Engineer |
Date & Time
| Template | Example Output |
|---|---|
| {{faker.date.past}} | 2024-01-15T10:30:00.000Z |
| {{faker.date.future}} | 2027-06-20T14:45:00.000Z |
| {{faker.date.recent}} | 2026-02-25T09:15:00.000Z |
| {{faker.date.month}} | March |
| {{faker.date.weekday}} | Monday |
Location
| Template | Example Output |
|---|---|
| {{faker.location.city}} | New York |
| {{faker.location.country}} | United States |
| {{faker.location.street}} | 123 Main Street |
| {{faker.location.zipCode}} | 10001 |
| {{faker.location.latitude}} | 40.7128 |
Internet
| Template | Example Output |
|---|---|
| {{faker.internet.url}} | https://example.com |
| {{faker.internet.email}} | user@example.com |
| {{faker.internet.username}} | john_doe123 |
| {{faker.internet.ip}} | 192.168.1.1 |
| {{faker.internet.uuid}} | a1b2c3d4-e5f6-7890-abcd-ef1234567890 |
Commerce & Finance
| Template | Example Output |
|---|---|
| {{faker.commerce.productName}} | Ergonomic Wooden Chair |
| {{faker.commerce.price}} | 42.00 |
| {{faker.finance.currencyCode}} | USD |
| {{faker.finance.bitcoinAddress}} | 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 |
Data Types
| Template | Example Output |
|---|---|
| {{faker.string.uuid}} | 550e8400-e29b-41d4-a716-446655440000 |
| {{faker.string.alpha}} | abcDEF |
| {{faker.string.numeric}} | 1234567890 |
| {{faker.datatype.boolean}} | true |
| {{faker.number.int}} | 42 |
Lorem (Text)
| Template | Example Output |
|---|---|
| {{faker.lorem.word}} | lorem |
| {{faker.lorem.sentence}} | Lorem ipsum dolor sit amet. |
| {{faker.lorem.paragraph}} | Multiple sentences... |
Example: Notification Endpoint
JSON
{
"path": "/ws/notifications",
"eventType": "message",
"response": {
"id": "{{faker.string.uuid}}",
"type": "notification",
"title": "{{faker.lorem.sentence}}",
"message": "{{faker.lorem.paragraph}}",
"user": {
"name": "{{faker.person.fullName}}",
"email": "{{faker.internet.email}}",
"avatar": "{{faker.image.avatar}}"
},
"read": "{{faker.datatype.boolean}}",
"createdAt": "{{faker.date.recent}}"
},
"delay": 1000
}Tip
You can also use simple format: {{faker.uuid}}, {{faker.email}}, {{faker.sentence}}, etc.
Info
For a complete reference of all available Faker templates, see the Faker Templates documentation.
Authentication
WebSocket endpoints can require authentication. You can enable this in the endpoint settings.
API Reference
WebSocket Endpoints
| Method | Route | Description |
|---|---|---|
| GET | /api/ws-endpoints | List all WebSocket endpoints |
| POST | /api/ws-endpoints | Create WebSocket endpoint |
| GET | /api/ws-endpoints/:id | List all WebSocket endpoints |
| PUT | /api/ws-endpoints/:id | Update WebSocket endpoint |
| DELETE | /api/ws-endpoints/:id | Delete WebSocket endpoint |
