# Eth.subscribe

The five kinds of `eth_subscribe`, each as a function that returns a `Stream` of the items the
node pushes.

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

const nextThree = Eth.subscribe.newHeads().pipe(
  Stream.map((head) => head.number),
  Stream.take(3),
  Stream.runCollect,
);
```

`nextThree` is an `Effect` that subscribes, collects the numbers of the next three blocks, and
unsubscribes. The [Subscriptions guide](https://ethect.dev/rpc/guides/subscriptions/) shows how to work with the
streams.

## Kinds

| Kind | `eth_subscribe` param | Item | Description |
| --- | --- | --- | --- |
| [`subscribe.newHeads`](https://ethect.dev/rpc/eth/subscribe/new-heads/) | `newHeads` | `Block.Header` | The header of each block added to the chain. |
| [`subscribe.logs`](https://ethect.dev/rpc/eth/subscribe/logs/) | `logs` | `Log` | The logs matching a filter, as blocks are added. |
| [`subscribe.newPendingTransactions`](https://ethect.dev/rpc/eth/subscribe/new-pending-transactions/) | `newPendingTransactions` | `Hash` or `Transaction` | The transactions entering the pending pool. |
| [`subscribe.transactionReceipts`](https://ethect.dev/rpc/eth/subscribe/transaction-receipts/) | `transactionReceipts` | `ReadonlyArray<Receipt>` | The receipts of each block added to the chain. |
| [`subscribe.syncing`](https://ethect.dev/rpc/eth/subscribe/syncing/) | `syncing` | sync status | The sync status, each time it changes. |

## What they share

- **A `Stream`.** Each kind returns a
  [`Stream<Item, Method.Error, Subscriptions>`](https://ethect.dev/rpc/subscription/#opening-a-subscription).
  Nothing is sent until the stream runs.
- **A transport that can push.** The stream needs the
  [`Subscriptions`](https://ethect.dev/reference/transport/#subscriptions) service, which the
  [WebSocket](https://ethect.dev/reference/transport/web-socket/), [IPC](https://ethect.dev/reference/transport/ipc/) and
  [EIP-1193](https://ethect.dev/reference/transport/eip1193/) transports provide. With the HTTP transport the
  program does not compile.
- **Unsubscribes when the stream ends.** When the stream ends or is interrupted, the transport
  sends `eth_unsubscribe`. `Stream.take(3)` ends it after three items.
- **Issued again after a reconnection.** When the connection drops and comes back, the
  subscription is sent again and the stream carries on. What the node emitted in between is
  not replayed. See [Connection lifecycle](https://ethect.dev/concepts/connection-lifecycle/).
- **New items only.** A subscription pushes what happens after it is opened. For what is
  already on chain, call a method: [`Eth.getLogs`](https://ethect.dev/rpc/eth/get-logs/),
  [`Eth.getBlockByNumber`](https://ethect.dev/rpc/eth/get-block-by-number/).
- **Apart from each other.** Several subscriptions share one connection with the calls, and
  each stream gets its own items.

## Providing a transport

A subscription runs like a method, with a transport that can push in place of HTTP.

```ts
import { Effect, Stream } from "effect";
import { Socket } from "effect/socket";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const program = Eth.subscribe.newHeads().pipe(
  Stream.take(3),
  Stream.runForEach((head) => Effect.log(`block ${head.number}`)),
);

await program.pipe(
  Effect.provide(Transport.webSocket("ws://127.0.0.1:8545")),
  Effect.provide(Socket.layerWebSocketConstructorGlobal),
  Effect.runPromise,
);
```

## Errors

A subscription fails like a method does, and the failure ends the stream.

| Error | When |
| --- | --- |
| `RpcError` | the node refuses the subscription, with the node's own code and reason |
| `CodecError` | the params cannot be encoded, or an item does not have the shape of its kind |
| `TransportError` | the connection is lost and the transport stops trying to reconnect |

## JSON-RPC method

`eth_subscribe` to open a subscription and `eth_unsubscribe` to close it. Neither is a function
of its own: the stream sends both.
