Docs/Server Events
Docs/Events

Server Events

Broadcast real-time events from your Node.js or Laravel backend to connected clients.

Server Events

Server events are triggered from your backend application and delivered to subscribed WebSocket clients or individual users in real time.

Note on App Key & App ID: In Socketo Cloud, your App Key and App ID use the same unified identifier (app.id). When initializing backend SDKs, pass this value to both appId and key options.


1. Triggering Events in Node.js

Using the official Pusher server SDK:

ts
import Pusher from 'pusher'

const pusher = new Pusher({
  appId: process.env.SOCKETO_APP_ID!,
  key: process.env.SOCKETO_APP_KEY!,
  secret: process.env.SOCKETO_APP_SECRET!,
  host: process.env.SOCKETO_HOST ?? 'api.socketo.dev',
  useTLS: true,
})

// Broadcast to a single channel
await pusher.trigger('orders', 'order-created', {
  orderId: 'ord_12345',
  amount: 49.99,
})

// Broadcast simultaneously to multiple channels (up to 100)
await pusher.trigger(['orders', 'analytics'], 'order-created', {
  orderId: 'ord_12345',
})

// Send an event directly to a signed-in user (delivered to pusher.user.bind)
await pusher.sendToUser('user_42', 'order-ready', {
  orderId: 'ord_12345',
  pickupTime: '15:30',
})

2. Triggering Events in Laravel

In Laravel, dispatch events implementing the ShouldBroadcast interface:

Event Class (app/Events/OrderCreated.php)

php
namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

class OrderCreated implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public function __construct(public Order $order) {}

    public function broadcastOn(): array
    {
        return [
            new Channel('orders'),
        ];
    }

    public function broadcastAs(): string
    {
        return 'order-created';
    }
}

Dispatching the Event

php
use App\Events\OrderCreated;

// Broadcasts immediately or via queue
OrderCreated::dispatch($order);

// Or using the broadcast helper
broadcast(new OrderCreated($order))->toOthers();

3. Socket ID Exclusion

When an action is initiated by a client (e.g. sending a chat message), pass the originating client's socket_id to prevent them from receiving a duplicate echo of their own action.

Node.js Example

ts
await pusher.trigger('team-chat', 'new-message', { text: 'Hello!' }, {
  socket_id: originatingSocketId,
})

Laravel Example

In Laravel, calling ->toOthers() automatically excludes the current user's socket connection from the broadcast:

php
broadcast(new MessageSent($message))->toOthers();

4. Batch Triggering

Trigger up to 10 distinct events across different channels in a single network round-trip:

ts
await pusher.triggerBatch([
  { channel: 'orders', name: 'order-processed', data: { id: 1 } },
  { channel: 'presence-chat', name: 'announcement', data: { text: 'Sale started!' } },
  { channel: 'metrics', name: 'stat-updated', data: { cpu: 42 } },
])

Direct HTTP REST API

If you are building custom backend integrations without an SDK or want to query channel occupancies and terminate user sockets, see the dedicated REST API Endpoints documentation.