PAIPAI

createPaiSocketIoServer()

Socket.IO door for the same createPai app. HTTP/SSE is the default.

Use this when the product already uses Socket.IO or its network buffers SSE events. Pair it with an HTTP receiver for commands, reads, and files; Socket.IO carries live updates over WebSocket or HTTP long-polling. For direct SSE, createPaiExpressReceiver() is sufficient. The public factory is createPaiSocketIoServer — there is no root createPaiSocketIoReceiver export.

import { createPaiSocketIoServer } from "@pai/receiver-socketio";

const io = await createPaiSocketIoServer({
  pai,
  resolveIdentity: async ({ request, agentId }) => ({
    identity: {
      userId: request.headers["x-user-id"] ?? "dev-user",
      workspaceId: request.headers["x-workspace-id"] ?? "dev",
    },
  }),
});

httpServer.on("request", (req, res) => {
  if (req.url?.startsWith(io.path)) io.handleRequest(req, res);
});
httpServer.on("upgrade", (req, socket, head) => {
  if (req.url?.startsWith(io.path)) io.handleUpgrade(req, socket, head);
});

Import

import { createPaiSocketIoServer } from "@pai/receiver-socketio";

Signature

function createPaiSocketIoServer(
  options: CreatePaiSocketIoServerOptions,
): Promise<PaiSocketIoServer>;

Parameters

options: CreatePaiSocketIoServerOptions

pai: Pai

resolveIdentity: ResolveSocketIoIdentity

Per-subscription identity. request is Node IncomingMessage.

path?: string

Defaults to /socket.io.

namespace?: string

Defaults to /pai.

redis?: Pick<RedisClientType, 'duplicate'>

Existing node-redis client. PAI calls duplicate() and owns those two connections; it does not close yours.

redisChannelPrefix?: string

Cluster channel prefix. Defaults from path and namespace.

socketOptions?: Partial<ServerOptions>

Native origin/CORS options. Host attach and serveClient are excluded.

watchLimits?: SocketIoWatchLimits

maxSubscriptions?: number

Defaults to 128.

authorizationTimeoutMs?: number

deliveryTimeoutMs?: number

logger?: PaiLogger | false

logLevel?: "debug" | "info" | "warn" | "error"

Returns

A PaiSocketIoServer.

handleRequest, handleUpgrade, and close(). Does not own the HTTP server.

Learn more

On this page