| 1 | import type * as ivschat from "@distilled.cloud/aws/ivschat"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { Room } from "./Room.ts"; |
| 5 | |
| 6 | /** |
| 7 | * The `roomIdentifier` is injected by the binding from the bound room; |
| 8 | * the caller supplies the event name and optional attributes. |
| 9 | */ |
| 10 | export interface SendEventRequest extends Omit< |
| 11 | ivschat.SendEventRequest, |
| 12 | "roomIdentifier" |
| 13 | > {} |
| 14 | |
| 15 | /** |
| 16 | * Send an application-defined event to every client connected to the bound |
| 17 | * room — the effectful call made from a deployed Lambda or Task. Use it to |
| 18 | * broadcast state changes (poll results, stream metadata, moderation |
| 19 | * notices) alongside user chat messages; `attributes` carries the payload |
| 20 | * as string key-value pairs. |
| 21 | * |
| 22 | * ### Broadcasting Events |
| 23 | * Provide the `SendEventHttp` implementation layer on the Function effect, |
| 24 | * bind the room in the init phase, then call the returned client at |
| 25 | * runtime. The binding grants `ivschat:SendEvent` on the room and injects |
| 26 | * its ARN as the `roomIdentifier` automatically. |
| 27 | * |
| 28 | * **Example:** Broadcast from a Lambda |
| 29 | * ```typescript |
| 30 | * // init |
| 31 | * const room = yield* IVSChat.Room("LiveChat"); |
| 32 | * const sendEvent = yield* IVSChat.SendEvent(room); |
| 33 | * |
| 34 | * return { |
| 35 | * fetch: Effect.gen(function* () { |
| 36 | * // runtime |
| 37 | * const { id } = yield* sendEvent({ |
| 38 | * eventName: "app:poll-result", |
| 39 | * attributes: { question: "q1", winner: "option-b" }, |
| 40 | * }); |
| 41 | * return HttpServerResponse.json({ id }); |
| 42 | * }), |
| 43 | * }; |
| 44 | * // on the Function effect: |
| 45 | * // .pipe(Effect.provide(IVSChat.SendEventHttp)) |
| 46 | * ``` |
| 47 | * |
| 48 | * @binding |
| 49 | */ |
| 50 | export interface SendEvent extends Binding.Service< |
| 51 | SendEvent, |
| 52 | "AWS.IVSChat.SendEvent", |
| 53 | ( |
| 54 | room: Room, |
| 55 | ) => Effect.Effect< |
| 56 | ( |
| 57 | request: SendEventRequest, |
| 58 | ) => Effect.Effect<ivschat.SendEventResponse, ivschat.SendEventError> |
| 59 | > |
| 60 | > {} |
| 61 | export const SendEvent = Binding.Service<SendEvent>("AWS.IVSChat.SendEvent"); |