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: noneEth.subscribe.newHeads.item; // the schema of a block header
// () => Stream<Block.Header, Method.Error, Subscriptions>const open = Eth.subscribe.newHeads;Functions
Section titled “Functions”| 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. |
Opening a subscription
Section titled “Opening a subscription”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:
- Subscribe. The params are encoded and
eth_subscribeis sent with the kind first. The node answers with a subscription id. - Stream. Each
eth_subscriptionnotification that carries that id is decoded through theitemschema and emitted. - Unsubscribe. When the stream ends, fails or is interrupted,
eth_unsubscribeis 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.
Properties
Section titled “Properties”- Type:
string(a literal, such as"newHeads")
The name of the kind: the first param of eth_subscribe.
params
Section titled “params”- 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 |