Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/smoldot/instance/bindings-smoldot-light.d.ts>Connection

Alternative client for Substrate-based chains.
Latest
interface Connection
import { type Connection } from "https://deno.land/x/smoldot@light-js-deno-v0.7.9/instance/bindings-smoldot-light.d.ts";

Connection to a remote node.

At any time, a connection can be in one of the three following states:

  • Opening (initial state)
  • Open
  • Reset

When in the Opening or Open state, the connection can transition to the Reset state if the remote closes the connection or refuses the connection altogether. When that happens, config.onReset is called. Once in the Reset state, the connection cannot transition back to another state.

Initially in the Opening state, the connection can transition to the Open state if the remote accepts the connection. When that happens, config.onOpen is called.

When in the Open state, the connection can receive messages. When a message is received, config.onMessage is called.

Methods

reset(streamId?: number): void

Transitions the connection or one of its substreams to the Reset state.

If the connection is of type "single-stream", the whole connection must be shut down. If the connection is of type "multi-stream", a streamId can be provided, in which case only the given substream is shut down.

The config.onReset or config.onStreamReset callbacks are not called.

The transition is performed in the background. If the whole connection is to be shut down, none of the callbacks passed to the Config must be called again. If only a substream is shut down, the onStreamReset and onMessage callbacks must not be called again with that substream.

send(data: Uint8Array, streamId?: number): void

Queues data to be sent on the given connection.

The connection must currently be in the Open state.

The streamId must be provided if and only if the connection is of type "multi-stream". It indicates which substream to send the data on.

openOutSubstream(): void

Start opening an additional outbound substream on the given connection.

The state of the connection must be Open. This function must only be called for connections of type "multi-stream".

The onStreamOpened callback must later be called with an outbound direction.

Note that no mechanism exists in this API to handle the situation where a substream fails to open, as this is not supposed to happen. If you need to handle such a situation, either try again opening a substream again or reset the entire connection.