@swim/client is a streaming API client for linking to lanes of stateful Web
Agents using the WARP protocol, enabling massively real-time applications that
continuously synchronize all shared states with ping latency. WARP is like
pub-sub without the broker, enabling every state of a Web API to be streamed,
without interference from billions of queues. @swim/client is part of the
@swim/mesh
framework.
A WarpRef is a handle through which WARP downlinks can be opened.
WarpClient implements the WarpRef interface, as does the exported
@swim/client module object, and by extension, the global swim namespace
object used web browsers and other non-module contexts.
WarpRef instances have four methods that open different kinds of downlinks.
The downlink method creates an EventDownlink for streaming raw events from
any Web Agent lane. The valueDownlink method creates a ValueDownlink for
synchronizing state with a Web Agent value lane. The mapDownlink method
creates a MapDownlink for synchronizing state with a Web Agent map lane.
And the listDownlink method creates a ListDownlink for synchronizing state
with a Web Agent list lane.
WarpRef instances can also be used to observe key lifecycle events.
The WarpRef.didConnect method registers an observer callback that
gets invoked whenever a connection to a WARP host is establishes.
The WarpRef.didDisconnect method registers an observer callback that
gets invoked whenever a WARP host disconnects. WarpRef.didAuthenticate
registers an observer callback that gets invoked whenever the client
successfully authenticates with a WARP host. WarpRef.didDeauthenticate
gets invoked when a WARP host rejects the client's authentication credentials.
And the WarpRef.didFail method registers an observer callback that gets
invoked when the client encounters an unexpected error.
The WarpClient class handles connection management and link routing,
and implements the WarpRef interface. In addition to opening downlinks,
WarpClient instances can be used to send arbitrary WARP commands, to provide
authentication credentials for hosts, to control network reconnection behavior,
and to create HostRef, NodeRef, and LaneRef scopes to facilitate downlink
management.
The WarpClient.authenticate method associates a credentials structure with
a particular host URI. The credentials will be sent in a WARP @auth envelope
whenever the client connects to the specified host.
The WarpClient.command method sends a WARP command message to a lane of
a remote node. WarpClient.command takes either three our four arguments.
The three argument command overload takes a node URI, a lane URI, and a
command payload. The node URI must have an authority component that specifies
the host to which the command should be sent. The four argument command
overload takes a host URI, a node URI, a lane URI, and a command payload;
the node URI is interpreted relative to the host URI.
The WarpClient.isOnline method returns true when the the client has
access to a network; it can also be used to force a client online or offline.
The WarpClient.keepOnline method controls whether or not the client should
automatically reopen connections after a network failure. Note that the
keepOnline state of the client overrides the keepLinked state of
individual downlinks. Setting keepOnline to false can be useful for
ephemeral clients, but should typically be left true.
swim.isOnline(); // true most of the timeswim.isOnline(false); // force offlineswim.isOnline(true); // force onlineswim.keepOnline(); // defaults to trueswim.keepOnline(false); // disable network reconnection
The WarpClient.hostRef method returns a new HostRef bound to the given
host URI. The WarpClient.nodeRef method returns a new NodeRef bound
to the given host and node URIs. The WarpClient.laneRef method returns
a new LaneRef bound to the given host, node, and lane URIs.
A HostRef is a WarpRef that automatically provides its bound host URI when
opening downlinks, sending commands, and providing authentication credentials.
HostRef instances keep track of all the downlinks they directly open. When
a HostRef is closed, it automatically closes all of its open downlink views.
A NodeRef is a WarpRef that automatically provides its bound host and node
URIs when opening downlinks and sending commands. NodeRef instances keep
track of all the downlinks they directly open. When a NodeRef is closed,
it automatically closes all of its open downlink views.
A LaneRef is a WarpRef that automatically provides its bound host, node,
and lane URIs when opening downlinks and sending commands. LaneRef instances
keep track of all the downlinks they directly open. When a LaneRef is closed,
it automatically closes all of its open downlink views.
A Downlink provides a virtual bidirectional stream between the client and a
lane of a remote Web Agent. WARP clients transparently multiplex all links to
Web Agents on a given host over a single WebSocket connection, and automatically
manage the network connection to each host, including reconnection and
resynchronization after a network failure. WARP clients also seamlessly handle
multicast event routing when multiple downlinks are opened to the same lane of
the same remote Web Agent.
Downlinks come in several flavors, depending on the WARP subprotocol to which
they conform. An EventDownlink observes raw WARP events, and can be used to
observe lanes of any kind. A ValueDownlink synchronizes a structured value
with a remote value lane. A MapDownlink implements the WARP map subprotocol
to synchronize key-value state with a remote map lane. A ListDownlink
implements the WARP list subprotocol to to synchronize sequential list state
with a remote list lane.
Before opening, a downlink must be addressed with the hostUri, nodeUri,
and laneUri to which the link should connect. A downlink may also be
configured with a relative priority, a max rate, and an optional body
structure that can contain query or other link parameters to be passed to the
remote lane.
The keepLinked parameter determines whether or not a downlink should be
automatically reopened after a network failure; it defaults to true. The
keepSynced parameter determines whether or not a downlink should synchronize
with the remote lane when opened; it defaults to true for stateful lanes.
The open method is used to open a downlink after it has been configured.
The close method closes a downlink. Closing a downlink does not necessarily
close the underlying WARP link. The WARP client will keep a link open so long
as at least one downlink to a given node and lane URI remains open. This
prevents application components from stepping on each other's toes when they
link to the same lanes of the same Web Agents. This can happen, for example,
when a UI has a summary view and a detail view both display information derived
from the same remote lane. The WARP link should not be closed when a detail
view is hidden, if state updates are still required by the summary view.
Events should also not be sent twice: once for the summary view, and once for
the detail view. Neither the summary view nor the detail view should have to
know about each other. And no global event dispatcher should be required,
which could introduce consistency problems. WARP clients efficiently, and
transparently handle all of these cases on behalf of all downlinks.
The isConnected method returns true if the underlying connection to the
remote host is currently open. The isAuthenticated method returns true
if the underlying connection to the remote host is currently authenticated.
The isLinked method returns true if the logical WARP link is currently
open. And the isSynced method returns true if the WARP link is currently
synchronized.
All downlinks support registering onEvent, onCommand, willLink, didLink,
willSync, didSync, willUnlink, didUnlink, willConnect, didConnect,
didDisconnect, didClose, and didFail callbacks.
A ValueDownlink synchronizes a shared real-time value with a remote value
lane. In addition to the standard Downlink callbacks, ValueDownlink
supports registering willSet and didSet callbacks to observe all changes
to downlinked state—whether remote or local.
A ValueDownlink views its state as a @swim/structureValue by default.
Use the valueForm method to create a typed projection of a ValueDownlink
that automatically transforms its state using a @swim/structureForm.
For example, you can use Form.foString() to create a ValueDownlink that
coerces its state to a string; and you can also use Form.forAny() to create
a ValueDownlink that coerces its state to a plain old JavaScript value.
Use the ValueDownlink.get method to get the current state value. Use the
ValueDownlink.set method to set the current state value.
value.get(); // get the current local state of the downlinkvalue.set(newValue); // update the local and remote state of the downlink
For the most part, client code can treat a ValueDownlink like an ordinary
mutable variable; the WARP client will ensure that the downlink is continuously
made consistent with the remote lane. Using didSet callbacks, applications
can update UI views, and other dependent components, to keep them consistent
with the shared state of the remote value lane in network real-time.
A MapDownlink synchronizes a shared real-time key-value map with a remote map
lane. In addition to the standard Downlink callbacks, MapDownlink supports
registering willUpdate, didUpdate, willRemove, and didRemove callbacks
to observe all changes to downlinked map state—whether remote or local.
A MapDownlink views its keys and values as @swim/structureValues by
default. Use the keyForm and valueForm methods to create a typed
projection of a MapDownlink that automatically transforms its keys and values
using @swim/structureForms.
MapDownlink implements the standard JavaScript Map interface. Use the
MapDownlink.get method to get the value associated with a given key. Use the
MapDownlink.set method to update the value associated with a key. And use
the MapDownlink.delete method to remove a key and its associated value.
map.get("kitchen"); // get the locally cached value associated with the keymap.set("garage", newRoom); // locally and remotely insert a new entry
For the most part, client code can treat a MapDownlink like an
ordinary JavaScript Map; the WARP client will ensure that the downlink is
continuously made consistent with the remote lane. Using didUpdate and
didRemove callbacks, applications can update UI collection views, and other
dependent components, to keep them consistent with the shared state of the
remote map lane in network real-time.
swim.downlinkMap() .didUpdate((key, value) => {if (hasChildElement(key)) {// update existing UI view for key } else {// insert new UI view for key } }) .didRemove((key) => {// remove UI view for key })
A ListDownlink synchronizes a shared real-time list with a remote list lane.
In addition to the standard Downlink callbacks, ListDownlink supports
registering willUpdate, didUpdate, willMove, didMove, willRemove,
and didRemove callbacks to observe all changes to downlinked list
state—whether remote or local.
A ListDownlink views its items as @swim/structureValues by default.
Use the valueForm method to create a typed projection of a ListDownlink
that automatically transforms its items using a @swim/structureForm.
ListDownlink behaves similarly to a JavaScript array. Use the
ListDownlink.get method to get the item at a given index. Use the
ListDownlink.set method to update the item at some index. And use the
ListDownlink.splice method to insert and remove items from the list.
You can also push, pop, shift, and unshift items, and move an
item from one index to another.
list.get(0); // get the first item in the listlist.set(0, "build"); // locally and remotely update an itemlist.push("paint"); // locally and remotely append an item
For the most part, client code can treat a ListDownlink like an ordinary
JavaScript list; the WARP client will ensure that the downlink is continuously
made consistent with the remote lane. Using didUpdate, didMove, and
didRemove callbacks, applications can update UI list views, and other
dependent components, to keep them consistent with the shared state of the
remote list lane in network real-time.
swim.downlinkList() .didUpdate((index, value) => {if (hasChildElement(index)) {// update existing UI view at index } else {// insert new UI view at index } }) .didMove((fromIndex, toIndex, value)) {// move existing UI view from old index to new index } .didRemove((index) => {// remove UI view at index })
For an npm-managed project, npm install @swim/client to make it a dependency.
TypeScript sources will be installed into node_modules/@swim/client/main.
Transpiled JavaScript and TypeScript definition files install into
node_modules/@swim/client/lib/main. And a pre-built UMD script can
be found in node_modules/@swim/client/dist/main/swim-client.js.
Browser applications can load swim-mesh.js—which bundles the @swim/client
library—along with its swim-core.js dependency, directly from the SwimOS CDN.
<!-- Development --><scriptsrc="https://cdn.swimos.org/js/latest/swim-core.js"></script><scriptsrc="https://cdn.swimos.org/js/latest/swim-mesh.js"></script><!-- Production --><scriptsrc="https://cdn.swimos.org/js/latest/swim-core.min.js"></script><scriptsrc="https://cdn.swimos.org/js/latest/swim-mesh.min.js"></script>
Alternatively, the standalone swim-system.js script may be loaded
from the SwimOS CDN, which bundles @swim/client together with all other
@swim/system
libraries.
<!-- Development --><scriptsrc="https://cdn.swimos.org/js/latest/swim-system.js"></script><!-- Production --><scriptsrc="https://cdn.swimos.org/js/latest/swim-system.min.js"></script>
When loaded by a web browser, the swim-mesh.js script adds all
@swim/client library exports to the global swim namespace. The
swim-mesh.js script requires that swim-core.js has already been loaded.
The swim-system.js script also adds all @swim/client library exports
to the global swim namespace, making it a drop-in replacement for
'swim-core.js' and swim-mesh.js when additional @swim/system
libraries are needed.
@swim/client
@swim/client is a streaming API client for linking to lanes of stateful Web Agents using the WARP protocol, enabling massively real-time applications that continuously synchronize all shared states with ping latency. WARP is like pub-sub without the broker, enabling every state of a Web API to be streamed, without interference from billions of queues. @swim/client is part of the @swim/mesh framework.
Overview
WarpRef
A
WarpRef
is a handle through which WARP downlinks can be opened.WarpClient
implements theWarpRef
interface, as does the exported @swim/client module object, and by extension, the globalswim
namespace object used web browsers and other non-module contexts.WarpRef
instances have four methods that open different kinds of downlinks. Thedownlink
method creates anEventDownlink
for streaming raw events from any Web Agent lane. ThevalueDownlink
method creates aValueDownlink
for synchronizing state with a Web Agent value lane. ThemapDownlink
method creates aMapDownlink
for synchronizing state with a Web Agent map lane. And thelistDownlink
method creates aListDownlink
for synchronizing state with a Web Agent list lane.WarpRef
instances can also be used to observe key lifecycle events. TheWarpRef.didConnect
method registers an observer callback that gets invoked whenever a connection to a WARP host is establishes. TheWarpRef.didDisconnect
method registers an observer callback that gets invoked whenever a WARP host disconnects.WarpRef.didAuthenticate
registers an observer callback that gets invoked whenever the client successfully authenticates with a WARP host.WarpRef.didDeauthenticate
gets invoked when a WARP host rejects the client's authentication credentials. And theWarpRef.didFail
method registers an observer callback that gets invoked when the client encounters an unexpected error.WarpClient
The
WarpClient
class handles connection management and link routing, and implements theWarpRef
interface. In addition to opening downlinks,WarpClient
instances can be used to send arbitrary WARP commands, to provide authentication credentials for hosts, to control network reconnection behavior, and to createHostRef
,NodeRef
, andLaneRef
scopes to facilitate downlink management.The
WarpClient.authenticate
method associates a credentials structure with a particular host URI. The credentials will be sent in a WARP@auth
envelope whenever the client connects to the specified host.Distinct
WarpClient
instances can be used to create isolated connection pools for different security domains.The
WarpClient.command
method sends a WARP command message to a lane of a remote node.WarpClient.command
takes either three our four arguments. The three argumentcommand
overload takes a node URI, a lane URI, and a command payload. The node URI must have an authority component that specifies the host to which the command should be sent. The four argumentcommand
overload takes a host URI, a node URI, a lane URI, and a command payload; the node URI is interpreted relative to the host URI.The
WarpClient.isOnline
method returnstrue
when the the client has access to a network; it can also be used to force a client online or offline. TheWarpClient.keepOnline
method controls whether or not the client should automatically reopen connections after a network failure. Note that thekeepOnline
state of the client overrides thekeepLinked
state of individual downlinks. SettingkeepOnline
to false can be useful for ephemeral clients, but should typically be lefttrue
.The
WarpClient.hostRef
method returns a newHostRef
bound to the given host URI. TheWarpClient.nodeRef
method returns a newNodeRef
bound to the given host and node URIs. TheWarpClient.laneRef
method returns a newLaneRef
bound to the given host, node, and lane URIs.HostRef
A
HostRef
is aWarpRef
that automatically provides its bound host URI when opening downlinks, sending commands, and providing authentication credentials.HostRef
instances keep track of all the downlinks they directly open. When aHostRef
is closed, it automatically closes all of its open downlink views.The
HostRef.nodeRef
andHostRef.laneRef
instance methods can be used to create further resolvedWarpRef
scopes.NodeRef
A
NodeRef
is aWarpRef
that automatically provides its bound host and node URIs when opening downlinks and sending commands.NodeRef
instances keep track of all the downlinks they directly open. When aNodeRef
is closed, it automatically closes all of its open downlink views.The
NodeRef.laneRef
instance method can be used to create further resolvedWarpRef
scopes.LaneRef
A
LaneRef
is aWarpRef
that automatically provides its bound host, node, and lane URIs when opening downlinks and sending commands.LaneRef
instances keep track of all the downlinks they directly open. When aLaneRef
is closed, it automatically closes all of its open downlink views.Downlink
A
Downlink
provides a virtual bidirectional stream between the client and a lane of a remote Web Agent. WARP clients transparently multiplex all links to Web Agents on a given host over a single WebSocket connection, and automatically manage the network connection to each host, including reconnection and resynchronization after a network failure. WARP clients also seamlessly handle multicast event routing when multiple downlinks are opened to the same lane of the same remote Web Agent.Downlinks come in several flavors, depending on the WARP subprotocol to which they conform. An
EventDownlink
observes raw WARP events, and can be used to observe lanes of any kind. AValueDownlink
synchronizes a structured value with a remote value lane. AMapDownlink
implements the WARP map subprotocol to synchronize key-value state with a remote map lane. AListDownlink
implements the WARP list subprotocol to to synchronize sequential list state with a remote list lane.Before opening, a downlink must be addressed with the
hostUri
,nodeUri
, andlaneUri
to which the link should connect. A downlink may also be configured with a relativeprio
rity, a maxrate
, and an optionalbody
structure that can contain query or other link parameters to be passed to the remote lane.The
keepLinked
parameter determines whether or not a downlink should be automatically reopened after a network failure; it defaults totrue
. ThekeepSynced
parameter determines whether or not a downlink should synchronize with the remote lane when opened; it defaults totrue
for stateful lanes.The
open
method is used to open a downlink after it has been configured. Theclose
method closes a downlink. Closing a downlink does not necessarily close the underlying WARP link. The WARP client will keep a link open so long as at least one downlink to a given node and lane URI remains open. This prevents application components from stepping on each other's toes when they link to the same lanes of the same Web Agents. This can happen, for example, when a UI has a summary view and a detail view both display information derived from the same remote lane. The WARP link should not be closed when a detail view is hidden, if state updates are still required by the summary view. Events should also not be sent twice: once for the summary view, and once for the detail view. Neither the summary view nor the detail view should have to know about each other. And no global event dispatcher should be required, which could introduce consistency problems. WARP clients efficiently, and transparently handle all of these cases on behalf of all downlinks.The
isConnected
method returnstrue
if the underlying connection to the remote host is currently open. TheisAuthenticated
method returnstrue
if the underlying connection to the remote host is currently authenticated. TheisLinked
method returnstrue
if the logical WARP link is currently open. And theisSynced
method returnstrue
if the WARP link is currently synchronized.All downlinks support registering
onEvent
,onCommand
,willLink
,didLink
,willSync
,didSync
,willUnlink
,didUnlink
,willConnect
,didConnect
,didDisconnect
,didClose
, anddidFail
callbacks.EventDownlink
An
EventDownlink
provides a raw view of a WARP link.ValueDownlink
A
ValueDownlink
synchronizes a shared real-time value with a remote value lane. In addition to the standardDownlink
callbacks,ValueDownlink
supports registeringwillSet
anddidSet
callbacks to observe all changes to downlinked state—whether remote or local.A
ValueDownlink
views its state as a @swim/structureValue
by default. Use thevalueForm
method to create a typed projection of aValueDownlink
that automatically transforms its state using a @swim/structureForm
. For example, you can useForm.foString()
to create aValueDownlink
that coerces its state to a string; and you can also useForm.forAny()
to create aValueDownlink
that coerces its state to a plain old JavaScript value.Use the
ValueDownlink.get
method to get the current state value. Use theValueDownlink.set
method to set the current state value.For the most part, client code can treat a
ValueDownlink
like an ordinary mutable variable; the WARP client will ensure that the downlink is continuously made consistent with the remote lane. UsingdidSet
callbacks, applications can update UI views, and other dependent components, to keep them consistent with the shared state of the remote value lane in network real-time.MapDownlink
A
MapDownlink
synchronizes a shared real-time key-value map with a remote map lane. In addition to the standardDownlink
callbacks,MapDownlink
supports registeringwillUpdate
,didUpdate
,willRemove
, anddidRemove
callbacks to observe all changes to downlinked map state—whether remote or local.A
MapDownlink
views its keys and values as @swim/structureValue
s by default. Use thekeyForm
andvalueForm
methods to create a typed projection of aMapDownlink
that automatically transforms its keys and values using @swim/structureForm
s.MapDownlink
implements the standard JavaScriptMap
interface. Use theMapDownlink.get
method to get the value associated with a given key. Use theMapDownlink.set
method to update the value associated with a key. And use theMapDownlink.delete
method to remove a key and its associated value.For the most part, client code can treat a
MapDownlink
like an ordinary JavaScriptMap
; the WARP client will ensure that the downlink is continuously made consistent with the remote lane. UsingdidUpdate
anddidRemove
callbacks, applications can update UI collection views, and other dependent components, to keep them consistent with the shared state of the remote map lane in network real-time.ListDownlink
A
ListDownlink
synchronizes a shared real-time list with a remote list lane. In addition to the standardDownlink
callbacks,ListDownlink
supports registeringwillUpdate
,didUpdate
,willMove
,didMove
,willRemove
, anddidRemove
callbacks to observe all changes to downlinked list state—whether remote or local.A
ListDownlink
views its items as @swim/structureValue
s by default. Use thevalueForm
method to create a typed projection of aListDownlink
that automatically transforms its items using a @swim/structureForm
.ListDownlink
behaves similarly to a JavaScript array. Use theListDownlink.get
method to get the item at a given index. Use theListDownlink.set
method to update the item at some index. And use theListDownlink.splice
method to insert and remove items from the list. You can alsopush
,pop
,shift
, andunshift
items, andmove
an item from one index to another.For the most part, client code can treat a
ListDownlink
like an ordinary JavaScript list; the WARP client will ensure that the downlink is continuously made consistent with the remote lane. UsingdidUpdate
,didMove
, anddidRemove
callbacks, applications can update UI list views, and other dependent components, to keep them consistent with the shared state of the remote list lane in network real-time.Installation
npm
For an npm-managed project,
npm install @swim/client
to make it a dependency. TypeScript sources will be installed intonode_modules/@swim/client/main
. Transpiled JavaScript and TypeScript definition files install intonode_modules/@swim/client/lib/main
. And a pre-built UMD script can be found innode_modules/@swim/client/dist/main/swim-client.js
.Browser
Browser applications can load
swim-mesh.js
—which bundles the @swim/client library—along with itsswim-core.js
dependency, directly from the SwimOS CDN.Alternatively, the standalone
swim-system.js
script may be loaded from the SwimOS CDN, which bundles @swim/client together with all other @swim/system libraries.Usage
ES6/TypeScript
@swim/client can be imported as an ES6 module from TypeScript and other ES6-compatible environments.
CommonJS/Node.js
@swim/client can also be used as a CommonJS module in Node.js applications.
Browser
When loaded by a web browser, the
swim-mesh.js
script adds all @swim/client library exports to the globalswim
namespace. Theswim-mesh.js
script requires thatswim-core.js
has already been loaded.The
swim-system.js
script also adds all @swim/client library exports to the globalswim
namespace, making it a drop-in replacement for 'swim-core.js' andswim-mesh.js
when additional @swim/system libraries are needed.