# Subscription definitions (Subscription)

A subscription definition. A kind of eth_subscribe with the schemas of its params and of the items the node pushes, which is also the function that opens it.

A `Subscription` is a kind of `eth_subscribe` with the schemas of its params and of the items
the node pushes. It is also the function that opens it, so each export of
[`Eth.subscribe`](https://ethect.dev/rpc/eth/subscribe/) is one.

```ts
import { Eth } from "@ethect/core/rpc";

Eth.subscribe.newHeads.kind; // "newHeads"
Eth.subscribe.newHeads.params; // the schema of the params after the kind: none
Eth.subscribe.newHeads.item; // the schema of a block header

// () => Stream<Block.Header, Method.Error, Subscriptions>
const open = Eth.subscribe.newHeads;
```

## Functions

| Function | Returns | Description |
| --- | --- | --- |
| [`Subscription.make<Names>()(kind, params, item)`](https://ethect.dev/rpc/subscription/make/) | `Subscription` | Defines a subscription kind, its params under the names given. |
| [`Subscription.overload(...variants)`](https://ethect.dev/rpc/subscription/overload/) | overloaded function | One kind whose items depend on its params. |

## Opening a subscription

A call takes the params of the kind and returns a `Stream`. Nothing is sent until the stream
is run.

```ts
import { Stream } from "effect";
import type { Transport } from "@ethect/core";
import type { Block, Method } from "@ethect/core/rpc";
import { Eth } from "@ethect/core/rpc";

const heads: Stream.Stream<Block.Header, Method.Error, Transport.Subscriptions> =
  Eth.subscribe.newHeads();
```

| Part | Type | Meaning |
| --- | --- | --- |
| item | the decoded item, here `Block.Header` | what the `item` schema produces for each push |
| error | [`Method.Error`](https://ethect.dev/rpc/method/#types) | the same three errors as a method |
| requirement | [`Subscriptions`](https://ethect.dev/reference/transport/#subscriptions) | provided by the transports that can push: WebSocket, IPC, EIP-1193 |

When the stream runs:

1. **Subscribe.** The params are encoded and `eth_subscribe` is sent with the kind first. The
   node answers with a subscription id.
2. **Stream.** Each `eth_subscription` notification that carries that id is decoded through
   the `item` schema and emitted.
3. **Unsubscribe.** When the stream ends, fails or is interrupted, `eth_unsubscribe` is sent
   with the id.

- **Ending a stream is how you unsubscribe.** `Stream.take(3)`, an interrupted fiber or a
  closed scope all do it. There is no handle to keep.
- **Reconnection.** On a socket transport, the subscription is issued again on each new
  connection and the stream carries on, also when the connection dropped before the node
  answered `eth_subscribe`. What the node emitted in between is not replayed. See
  [Connection lifecycle](https://ethect.dev/concepts/connection-lifecycle/).
- **A refused subscription fails the stream** with the node's `RpcError`.
- **An item of the wrong shape fails the stream** with a `CodecError`.
- **Unbounded buffer.** Items wait in memory until the consumer takes them. A slow consumer
  of a busy subscription grows the buffer.

## Properties

### kind

- **Type:** `string` (a literal, such as `"newHeads"`)

The name of the kind: the first param of `eth_subscribe`.

### params

- **Type:** an Effect `Schema`

The schema of the params after the kind, as a tuple.

### item

- **Type:** an Effect `Schema`

The schema of each item pushed.

```ts
import { Effect, Schema } from "effect";
import { Eth } from "@ethect/core/rpc";

// Decode a log you received some other way with the schema of the subscription.
const decodeLog = Schema.decodeUnknownEffect(Eth.subscribe.logs.item);
```

## Types

| Type | Is |
| --- | --- |
| `Subscription.Subscription<Kind, Params, Item, Names>` | a definition: the function plus `kind`, `params` and `item`. `Names` holds the [names of its params](https://ethect.dev/rpc/subscription/make/#names) |
| `Subscription.Any` | any subscription, whatever its schemas |
