Skip to main content
The Realtime component lets you add pub/sub messaging to your app using WebSockets. It uses Amazon API Gateway WebSocket API.

Constructor

sst.config.ts
const realtime = new sst.aws.Realtime("MyRealtime", {
  authorizer: "src/authorizer.handler"
});

Parameters

authorizer

authorizer
string | FunctionArgs
The Lambda function to authorize connections.
{
  authorizer: "src/authorizer.handler"
}

Properties

url

nodes

SDK

Publish messages to connected clients:
src/publisher.ts
import { Resource } from "sst";
import { ApiGatewayManagementApiClient, PostToConnectionCommand } from "@aws-sdk/client-apigatewaymanagementapi";

const client = new ApiGatewayManagementApiClient({
  endpoint: Resource.MyRealtime.url
});

await client.send(new PostToConnectionCommand({
  ConnectionId: connectionId,
  Data: JSON.stringify({ message: "Hello" })
}));

Examples

Create a realtime component

sst.config.ts
const realtime = new sst.aws.Realtime("MyRealtime", {
  authorizer: "src/authorizer.handler"
});

realtime.subscribe("$default", "src/default.handler");
realtime.subscribe("sendMessage", "src/sendMessage.handler");

Build docs developers (and LLMs) love