---
title: Push to connected apps
description: Send typed messages from a luna to the apps connected to it, to all of them or to the ones you tag.
related:
  - /realtime/open-a-connection
  - /realtime/follow-a-stream-from-an-app
  - /recipes/build-a-chat-room
---

A live query shows data, but some news is not data: someone joined, someone is typing, a game starts. A push is a typed message a luna sends to the apps connected to it, at the moment something happens.

## Declare a push and send it

A luna type declares its pushes, each with a type from [`t`](/sdk/t). `conn.push` sends one to a single connection.

```ts luna/lunas/room.ts
import { defineLuna, t } from "luna";

export const Room = defineLuna({
  name: "Room",
  visibility: "public",
  key: { id: t.string() },
  pushes: {
    said: t.object({ from: t.string(), text: t.string() }),
  },
  hooks: {
    onConnect(ctx, conn) {
      conn.push("said", { from: "room", text: "welcome" });
    },
  },
  handlers: (h) => ({
    say: h({ args: { text: t.string() } }, (ctx, { text }) => {
      ctx.broadcast("said", { from: "someone", text });
    }),
  }),
});
```

In the app, `on` on a [`LunaHandle`](/sdk/luna-handle) hears a push by its name. It opens the connection to the luna, and keeps listening across reconnects until you call the function it returns.

```ts app/room.ts
import { createClient } from "luna/client";
import type { Room } from "../luna/lunas/room.ts";

const client = createClient({ url: "<LUNA_URL>", key: "<LUNA_KEY>" });
const room = client.luna<typeof Room>("Room", "lobby");

const stop = room.on<{ from: string; text: string }>("said", (said) => {
  console.log(`${said.from}: ${said.text}`);
});

await room.mutate("say", { text: "hello" });
```

The app logs the welcome when it connects, then the message it said. `conn.push` returns `false` when the connection has already closed.

## Push to every connected app

`ctx.broadcast` sends a push to every connection of the luna, as `say` does above. Each app connected to the `lobby` room hears it once.

## Push to some apps

`ctx.broadcast` takes options that pick the connections.

| Option | Does |
| --- | --- |
| `tags` | Sends only to connections with any of these tags |
| `except` | Leaves out one connection, given as a `Connection` or its id |

Leave out `ctx.connection` to skip the app that made the call. Tag each connection with its person in `onConnect`, as [Open a connection](/realtime/open-a-connection) shows, and a push can reach every device of one person.

```ts luna/lunas/room.ts
say: h({ args: { text: t.string() } }, (ctx, { text }) => {
  const from = "external" in ctx.caller ? (ctx.caller.external as { name: string }).name : "room";
  ctx.broadcast("said", { from, text }, { except: ctx.connection });
}),
notify: h({ args: { to: t.string(), text: t.string() } }, (ctx, { to, text }) => {
  ctx.broadcast("said", { from: "room", text }, { tags: [`user:${to}`] });
}),
```

## Know when a push leaves

A push leaves once the handler that sent it ends well and its writes are stored. A handler that fails sends none of its pushes, so an app never hears of a change that did not happen.

Each push is checked against its declared type when it is sent. An undeclared name, data of the wrong type or data over 1 MiB as JSON fails the handler with `Invalid`.

## Keep what an app must not miss

:::warning
Luna does not keep pushes. An app that is offline or reconnecting when a push is sent never receives it. An app that reads too slowly, with 4,096 messages waiting, is disconnected with code 1013.
:::

Push the news, and keep the record in a table or a stream. An app that comes back reads the table with a [live query](/realtime/subscribe-to-a-query), or the stream from where it stopped, as [Follow a stream from an app](/realtime/follow-a-stream-from-an-app) shows.

## Hear pushes without a WebSocket

A `GET` to the luna's `connect` path that accepts `text/event-stream` opens a one-way connection over server-sent events. It runs `onBeforeConnect` and `onConnect` like a WebSocket connection, and closes when the reader stops reading.

```bash
curl -N "$LUNA_URL/Room/lobby/connect?key=$LUNA_KEY" -H "Accept: text/event-stream"
```

The stream starts with an `open` event. Each push then arrives as an event named `event`, whose data is `{"type":"event","event":"said","data":{...}}`. A `close` event ends the stream when the luna closes the connection.

## Next steps

- [Follow a stream from an app](/realtime/follow-a-stream-from-an-app) reads messages an app must not miss.
- [Build a chat room](/recipes/build-a-chat-room) puts connections, pushes and live queries together.
- [Use Luna with React](/clients/use-luna-with-react) listens to pushes from a component.
