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

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

Alternative client for Substrate-based chains.
Go to Latest
interface Connection
import { type Connection } from "https://deno.land/x/smoldot2@light-js-deno-v1.0.1/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 and stream must currently be in the Open state.

The number of bytes most never exceed the number of "writable bytes" of the stream. onWritableBytes can be used in order to notify that more writable bytes are available.

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.

Must not be called after closeSend has been called.

closeSend(streamId?: number): void

Closes the writing side of the given stream of the given connection.

Never called for connection types where this isn't possible to implement (i.e. WebSocket and WebRTC at the moment).

The connection and stream must currently be in the Open state.

Implicitly sets the "writable bytes" of the stream to zero.

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.

Must only be called once per stream.

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.