Skip to content

Subscription definitions (Subscription)

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 is one.

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;
Function Returns Description
Subscription.make<Names>()(kind, params, item) Subscription Defines a subscription kind, its params under the names given.
Subscription.overload(...variants) overloaded function One kind whose items depend on its params.

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

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 the same three errors as a method
requirement 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.
  • 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.
  • Type: string (a literal, such as "newHeads")

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

  • Type: an Effect Schema

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

  • Type: an Effect Schema

The schema of each item pushed.

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);
Type Is
Subscription.Subscription<Kind, Params, Item, Names> a definition: the function plus kind, params and item. Names holds the names of its params
Subscription.Any any subscription, whatever its schemas