# Transport.ipc

A transport over a node's IPC socket, for a node on the same machine. It serves calls and
subscriptions on one connection and reconnects when it drops.

## Usage

```ts
import { NodeSocket } from "@effect/platform-node";
import { Effect, Layer } from "effect";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const Ipc = Transport.ipc().pipe(
  Layer.provide(NodeSocket.layerNet({ path: "/tmp/anvil.ipc" })),
);

const block = await Eth.blockNumber().pipe(Effect.provide(Ipc), Effect.runPromise);
```

Opening a socket is the platform's job, so the transport takes no path: the `Socket` layer
does. On Node it is `NodeSocket.layerNet` from `@effect/platform-node`.

Start anvil with a socket to try it:

```sh
anvil --ipc /tmp/anvil.ipc
```

### Subscriptions

An IPC connection can push, so subscriptions work as over WebSocket.

```ts
import { NodeSocket } from "@effect/platform-node";
import { Effect, Layer, Stream } from "effect";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const Ipc = Transport.ipc().pipe(
  Layer.provide(NodeSocket.layerNet({ path: "/tmp/anvil.ipc" })),
);

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

## Returns

`Layer<Transport | Subscriptions, never, Socket>`

A layer that provides [`Transport`](https://ethect.dev/reference/transport/#transport) and
[`Subscriptions`](https://ethect.dev/reference/transport/#subscriptions), and needs a `Socket` from
`effect/socket`.

## Parameters

### options (optional)

- **Type:** `Transport.SocketOptions`

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `reconnect` | a `Schedule` | exponential backoff from 500 ms, capped at 5 s, forever | The pauses between attempts to connect, after one fails or a connection ends within 5 seconds of opening. When the schedule ends, the transport gives up. |
| `keepAlive` | a `Duration` input, or `false` | off | How often `net_version` is called to check the connection. |

```ts
import { NodeSocket } from "@effect/platform-node";
import { Layer, Schedule } from "effect";
import { Transport } from "@ethect/core";

const Ipc = Transport.ipc({
  reconnect: Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(10)]),
}).pipe(Layer.provide(NodeSocket.layerNet({ path: "/tmp/anvil.ipc" })));
```

## Behavior

- **Same connection rules as WebSocket.** Calls in flight fail with `ConnectionLost` when the
  socket closes, a connection that lasted 5 seconds or more is replaced at once, calls fail
  at once with `Unreachable` while the node cannot be reached, and subscriptions are issued
  again after a reconnection. See
  [`Transport.webSocket`](https://ethect.dev/reference/transport/web-socket/#behavior).
- **No keep-alive by default.** Set `keepAlive` to add one.
- **Framing.** Each request is one JSON text followed by a newline. Answers are split on the
  boundaries of JSON values, so a value cut across two chunks or several values in one chunk
  are both read correctly.

## Errors

The same as [`Transport.webSocket`](https://ethect.dev/reference/transport/web-socket/#errors): `Unreachable`,
`ConnectionLost`, `MalformedResponse` and `Unserializable`.
