Skip to main content

Watch Collection

Subscribe to real-time changes in a collection using WebSockets or Server-Sent Events.

WebSocket Connection

Endpoint

wss://api.cocobase.buzz/realtime

Authentication

Send authentication message after connecting:
{
  "type": "auth",
  "token": "YOUR_API_KEY"
}

Subscribe to Collection

{
  "type": "subscribe",
  "collection": "posts",
  "filters": {
    "status": "published"
  }
}

Event Messages

{
  "type": "event",
  "collection": "posts",
  "event": "created",
  "document": {
    "id": "doc_abc123",
    "data": {
      "title": "New Post"
    },
    "createdAt": "2024-01-15T10:30:00Z"
  }
}

Event Types

EventDescription
createdNew document created
updatedDocument modified
deletedDocument deleted

JavaScript Example

const ws = new WebSocket("wss://api.cocobase.buzz/realtime");

ws.onopen = () => {
  // Authenticate
  ws.send(JSON.stringify({
    type: "auth",
    token: "YOUR_API_KEY",
  }));

  // Subscribe to collection
  ws.send(JSON.stringify({
    type: "subscribe",
    collection: "posts",
    filters: {
      status: "published",
    },
  }));
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);

  if (data.type === "event") {
    console.log(`${data.event}: ${data.document.id}`);

    switch (data.event) {
      case "created":
        console.log("New document:", data.document);
        break;
      case "updated":
        console.log("Updated document:", data.document);
        break;
      case "deleted":
        console.log("Deleted document:", data.document.id);
        break;
    }
  }
};

ws.onerror = (error) => {
  console.error("WebSocket error:", error);
};

ws.onclose = () => {
  console.log("Connection closed");
};

Server-Sent Events (SSE)

Alternative to WebSockets for simpler implementations.

Endpoint

GET /realtime/sse/{collection}

Query Parameters

ParameterTypeDescription
filtersstringJSON-encoded filters

Example

curl -N "https://api.cocobase.buzz/realtime/sse/posts" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Accept: text/event-stream"

JavaScript with EventSource

const params = new URLSearchParams({
  filters: JSON.stringify({ status: "published" }),
});

const eventSource = new EventSource(
  `https://api.cocobase.buzz/realtime/sse/posts?${params}`,
  {
    headers: {
      Authorization: "Bearer YOUR_API_KEY",
    },
  }
);

eventSource.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log(`${data.event}: ${data.document.id}`);
};

eventSource.onerror = (error) => {
  console.error("SSE error:", error);
};

// Close connection
eventSource.close();

Unsubscribe

WebSocket

{
  "type": "unsubscribe",
  "collection": "posts"
}

Example

// Unsubscribe from collection
ws.send(JSON.stringify({
  type: "unsubscribe",
  collection: "posts",
}));

// Close connection
ws.close();

Error Messages

Authentication Error

{
  "type": "error",
  "code": "UNAUTHORIZED",
  "message": "Invalid or missing authentication token"
}

Invalid Collection

{
  "type": "error",
  "code": "INVALID_COLLECTION",
  "message": "Collection does not exist"
}