---
title: "client.LiveEvents"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://nextmail.sh/docs/go/reference/live-events"
area: "Go"
category: "Reference"
---

# client.LiveEvents

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

Follow the mailbox as it changes, without polling: one event each time a message arrives or is sent and each time a conversation changes or is deleted, resumed where it left off after a dropped connection.

### `LiveEvents.Stream`

Follow the mailbox as it changes

```go
Stream(ctx context.Context, opts ...nextmail.RequestOption) *nextmail.LiveEventStream
```

Opens the live event stream, `GET /live-events` as server-sent events, and delivers one event at a time for as long as you read it: `message.received` when a message is filed for an address, `message.sent` when a message sent from an address is filed in its Sent folder, `thread.changed` when a conversation gains or loses labels (read, starred, archived, moved, snoozed or labelled), `thread.deleted` when one is deleted for good, and `message.status_changed` when the delivery of an email sent from an address moves on, with its `status`: `delivered`, `delivery_delayed`, `bounced`, `complained`, `failed`, `suppressed` or `cancelled`. Each event is a `nextmail.LiveEvent` with `ID`, `Type`, `OccurredAt` and `Data`, where `Data` is a `nextmail.Object` whose `addresses` names the addresses it concerns and whose `threadId` and `messageId` name the conversation and the message, where there is one. No content travels in an event, so read the conversation with `Threads.Get` or the message with `Threads.GetMessage` when you need it.

The stream never ends by itself. The server closes it after 30 minutes, when the credential stops being valid, or when the connection drops, and the stream opens it again and resumes where it left off through `Last-Event-ID`: at once after the server closes it, and with backoff after a failure. When the server can no longer replay from that point, which happens once the event is more than 10 minutes or 1,000 events back, the stream delivers an event whose `Type` is `nextmail.LiveEventReset` before the next events: anything may have changed in between, so list again what you keep in sync. Its `ID` is the point the stream carries on from, and its `Data` holds `reason`, `expired` or `unknown`.

Range over `All()`, or call `Next`, `Event` and `Err` in a plain loop. Leaving the loop of `All()` closes the stream, and so do `Close` and cancelling the context, all three without an error. `Close` may be called from another goroutine to stop a loop that is waiting for the next event.

A key or an app limited to particular addresses gets only the events of the addresses it reaches, enforced on the server for every event.

Scopes: `threads:read`.

**Parameters**

- `nextmail.WithTypes` (`...string`): Only these kinds of event, the `constants.LiveEventTypesValues`: `message.received`, `message.sent`, `thread.changed`, `thread.deleted` and `message.status_changed`. Left out, all of them. A reset is always delivered.
- `nextmail.WithAddress` (`string`): Only the events of this address. One the key does not reach delivers nothing rather than failing.
- `nextmail.WithLastEventID` (`string`): Resume after this event id, the `ID` of an event an earlier stream delivered, sent as the `Last-Event-ID` header. Left out, the stream starts with what happens from now on.
- `nextmail.WithAPIKey` (`string`): Overrides the client's API key for this stream only.

**Returns**

The `*nextmail.LiveEventStream`, delivering each event as a `nextmail.LiveEvent` with `ID`, `Type`, `OccurredAt` and `Data`. `Data` holds `addresses`, `threadId` and `messageId` and, by type, `labelIds` (received), `emailId` (sent), `addedLabelIds` and `removedLabelIds` (changed), or `emailId`, `status` and `recipients` (status changed, where `threadId` may be null). A reset holds only `reason`.

**Example**

```go
for event, err := range client.LiveEvents.Stream(ctx, nextmail.WithTypes(constants.LiveEventTypesMessageReceived)).All() {
	if err != nil {
		return err
	}

	if event.Type == nextmail.LiveEventReset {
		fmt.Println("Resync: the stream could not resume")

		continue
	}

	thread, err := client.Threads.Get(ctx, event.Data.String("threadId"))
	if err != nil {
		return err
	}

	fmt.Println("New mail for", event.Data.Strings("addresses"), "in", thread.ID())

	return nil
}
```

**Notes**

- One key, app or session holds at most 5 open streams, and a workspace at most 50. Past that the stream is refused with 429 `too_many_streams`.
- The server sends a keepalive every 25 seconds and a `retry` hint of 3 seconds. Events are kept for resuming for 10 minutes and at most 1,000 per workspace.
- A network failure and a 408, 429 or 5xx answer are retried for as long as you read, waiting about a second at first and doubling up to 30 seconds, or longer when `Retry-After` asks, up to 5 minutes. `nextmail.WithMaxRetries` does not limit it. When no byte arrives for 60 seconds, the stream drops the connection and opens it again.
- A 400, 401, 403, 404 or 422 ends the stream with the `*nextmail.Error` that `Err` returns instead of retrying, so an expired or revoked credential ends it on the next attempt to open it. An answer that is not an event stream ends it the same way, with the code `unrecognised_response`.
- The client timeout, or `nextmail.WithTimeout` on the call, bounds only the wait for the stream to open, not how long it stays open.

Also available in: API [`GET /live-events`](https://nextmail.sh/docs/api/reference/live-events#get-live-events); TypeScript [`liveEvents.stream()`](https://nextmail.sh/docs/sdk/reference/live-events#stream); Python [`live_events.stream()`](https://nextmail.sh/docs/python/reference/live-events#stream); Ruby [`live_events.stream`](https://nextmail.sh/docs/ruby/reference/live-events#stream); PHP [`liveEvents->stream`](https://nextmail.sh/docs/php/reference/live-events#stream); Java [`liveEvents().stream`](https://nextmail.sh/docs/java/reference/live-events#stream); C# [`LiveEvents.StreamAsync`](https://nextmail.sh/docs/csharp/reference/live-events#stream); CLI [`nextmail live-events stream`](https://nextmail.sh/docs/cli/reference/live-events#live-events-stream).
