# Eth.subscribe.syncing

A stream of the node's sync status, pushed each time the node starts or stops syncing.

## Usage

```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.syncing().pipe(
  Stream.map((status) => (status === false ? "in sync" : "syncing")),
  Stream.take(1),
  Stream.runCollect,
);

const statuses = await program.pipe(
  Effect.provide(Transport.webSocket("ws://127.0.0.1:8545")),
  Effect.provide(Socket.layerWebSocketConstructorGlobal),
  Effect.runPromise,
);
// ["in sync"]
```

anvil is never syncing: it pushes `false` once when the subscription opens, and nothing after.

A subscription needs a transport that can push: WebSocket here, or IPC or EIP-1193. The
examples below leave the transport out.

### Reading the progress

An item has one of three shapes. `false` means in sync, and the two others carry the progress,
directly or under `status`.

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

const progress = Eth.subscribe.syncing().pipe(
  Stream.map((item) => {
    if (item === false) return null;
    // geth wraps the progress while it syncs
    if ("status" in item) return item.syncing ? item.status : null;
    return item;
  }),
  Stream.map((status) =>
    status === null ? "in sync" : `${status.currentBlock} of ${status.highestBlock}`,
  ),
);
```

### Waiting for the node to be in sync

Take items until one says the node is done.

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

const synced = Eth.subscribe.syncing().pipe(
  Stream.filter((item) =>
    item === false || ("status" in item && !item.syncing),
  ),
  Stream.take(1),
  Stream.runDrain,
);
```

To know the status right now, without waiting for a change, call
[`Eth.syncing`](https://ethect.dev/rpc/eth/syncing/).

## Returns

[`Stream<SyncStatus.SyncStatus | { syncing: boolean, status: SyncStatus.Progress }, Method.Error, Subscriptions>`](https://ethect.dev/rpc/subscription/#opening-a-subscription)

Each item is one of:

| Item | Meaning |
| --- | --- |
| `false` | the node is in sync |
| a [`SyncStatus.Progress`](https://ethect.dev/rpc/types/sync-status/) | the node is syncing: `currentBlock`, `highestBlock` and, where the client reports it, `startingBlock`, as `bigint` |
| an object with `syncing` and `status` | the form geth pushes while it syncs: `status` is the progress |

## Parameters

None.

## Behavior

- **Changes only.** The node pushes when it starts or stops syncing, not on every block it
  imports. A node that is in sync and stays so pushes nothing more.
- **Two forms on the wire.** A node pushes the progress as it is, or as geth does, wrapped in
  an object. The schema accepts both.
- **Unsubscribes when the stream ends.** When the stream ends or is interrupted, the transport
  sends `eth_unsubscribe`.
- **Carries on over a reconnection.** The subscription is issued again on the new connection.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. Here they fail the stream:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | the node's own | the node refuses the subscription |
| `CodecError` | `on: "result"` | an item has none of the three shapes |
| `TransportError` | `Unreachable` | the connection is lost and the reconnect schedule runs out |

## JSON-RPC method

`eth_subscribe` with the kind `syncing`
