hypercore-substream
A hypercore extension that allows you to create and tunnel virtual streams over a replication channel.
Usage
const hypercore = const SubstreamRouter = // Setup two feeds and two vnetsconst localFeed = const vnetA = localFeed const remoteFeed = const vnetB = remoteFeed // Start replicationconst replicationStream = localFeedreplicationStream // Open a substream by specifying identical namespaceconst localSub = vnetAconst remoteSub = vnetB remoteSub
Should print
> Recv data: Hello Underworld
API
SubstreamRouter
class: const vnet = core.registerExtension(new SubstreamRouter(opts = {}))
The router is responsible for creating new and multiplexing existing substreams.
In order to be usable it needs to be passed through a registerExtension()
method
by something that supports hypercore extensions.
Handlers
when initializing a new SubstreamRouter
you can optionally pass
the following handlers:
opts.onsubdiscovery: function (namespace, peer)
Listen for new substreamsopts.onsubconnect: function(sub)
When a substream becomes establishedopts.onsubclose: function(sub)
When a substream is closed
vnet.open(namespace, opts = {}, callback)
Creates new substreams
Arguments
namespace
a string or buffer identifying the channel.opts
Objectopts.timeout
Number signifying time to wait before HandshakeTimeoutError' is emitted
callback
optionalfunction (error, virtualStream)
If provided, will be called when stream becomes either active or fails to initialize.
Returns
a full-duplex node stream.
Substream
event connected
Emitted when a connection has been established on both peer's ends.
Note: the sub stream is initialized in corked and paused state.
It is resumed and uncorked after the connected
event has been fired.
License
MIT