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:

  1. Navigate to the WebSocket section in the sidebar
  2. Click “Add WebSocket Endpoint”
  3. Configure the path (e.g., /ws/chat)
  4. Set the event type and response
  5. 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

TemplateExample 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

TemplateExample 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

TemplateExample 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

TemplateExample 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

TemplateExample Output
{{faker.commerce.productName}}Ergonomic Wooden Chair
{{faker.commerce.price}}42.00
{{faker.finance.currencyCode}}USD
{{faker.finance.bitcoinAddress}}1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2

Data Types

TemplateExample 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)

TemplateExample 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

MethodRouteDescription
GET/api/ws-endpointsList all WebSocket endpoints
POST/api/ws-endpointsCreate WebSocket endpoint
GET/api/ws-endpoints/:idList all WebSocket endpoints
PUT/api/ws-endpoints/:idUpdate WebSocket endpoint
DELETE/api/ws-endpoints/:idDelete WebSocket endpoint