8.5.5 • Published 3 months ago

streamr-client v8.5.5

Weekly downloads
1,040
License
Apache-2.0
Repository
github
Last release
3 months ago

Build Status

Streamr JavaScript Client

By using this client, you can easily interact with the Streamr API from JavaScript-based environments, such as browsers and node.js. You can, for example, subscribe to real-time data in Streams, produce new data to Streams, and create new Streams.

This library is work-in-progress and doesn't provide wrapper functions for all the endpoints in the Streamr API. Currently it covers producing and subscribing to data as well as manipulating Stream objects.

The client uses websockets for producing and consuming messages to/from streams. It should work in all modern browsers.

Installation

The client is available on npm and can be installed simpy by:

npm install streamr-client

Usage

Here are some quick examples. More detailed examples for the browser and node.js can be found here.

Creating a StreamrClient instance

const client = new StreamrClient({
    // See below for more options
    auth: {
        apiKey: 'your-api-key'
    }
})

Subscribing to real-time events in a stream

const sub = client.subscribe(
    {
        stream: 'streamId',
        apiKey: 'secret',       // Optional. If not given, uses the apiKey given at client creation time.
        partition: 0,           // Optional, defaults to zero. Use for partitioned streams to select partition.
        // optional resend options here
    },
    (message, metadata) => {
        // This is the message handler which gets called for every incoming message in the Stream.
        // Do something with the message here!
    }
)

Programmatically creating a Stream

client.getOrCreateStream({
    name: 'My awesome Stream created via the API',
})
    .then((stream) => {
        console.log(`Stream ${stream.id} has been created!`)
        // Do something with the Stream, for example call stream.publish(message)
    })

Producing data points to a Stream

// Here's our example data point
const msg = {
    temperature: 25.4,
    humidity: 10,
    happy: true
}

// Publish using the Stream id only
client.publish('my-stream-id', msg)

// Or alternatively, via the Stream object (from e.g. getOrCreateStream)
stream.publish(msg)

Client options

OptionDefault valueDescription
urlwss://www.streamr.com/api/v1/wsAddress of the Streamr websocket endpoint to connect to.
restUrlhttps://www.streamr.com/api/v1/wsBase URL of the Streamr REST API.
auth{}Object that can contain different information to authenticate. More details below.
publishWithSignature'auto'Determines if data points published to streams are signed or not. Possible values are: 'auto', 'always' and 'never'. Signing requires auth.privateKey or auth.provider. 'auto' will sign only if one of them is set. 'always' will throw an exception if none of them is set.
verifySignatures'auto'Determines under which conditions signed and unsigned data points are accepted or rejected. 'always' accepts only signed and verified data points. 'never' accepts all data points. 'auto' verifies all signed data points before accepting them and accepts unsigned data points only for streams not supposed to contain signed data.
autoConnecttrueIf set to true, the client connects automatically on the first call to subscribe(). Otherwise an explicit call to connect() is required.
autoDisconnecttrue  If set to true, the client automatically disconnects when the last stream is unsubscribed. Otherwise the connection is left open and can be disconnected explicitly by calling disconnect().

Authentication options

OptionDefault valueDescription
auth.apiKeynullDefault API key to use to authenticate.
auth.privateKeynullEthereum private key to use to authenticate.
auth.providernullEthereum provider used to connect to an account to use to authenticate.
auth.usernamenullUsername to use to authenticate. Needs auth.password as well.
auth.passwordnullPassword to use to authenticate. Needs auth.username as well.
auth.sessionTokennullSession token to authenticate directly without fetching a token with credentials. If the token expires, a new token cannot be retrieved.

Message handler callback

The second argument to client.subscribe(options, callback) is the callback function that will be called for each message as they arrive. Its arguments are as follows:

ArgumentDescription
messageA javascript object containing the message itself
metadataMetadata for the message, for example metadata.timestamp etc.

StreamrClient object

Connecting

NameDescription
connect()Connects to the server, and also subscribes to any streams for which subscribe() has been called before calling connect().
disconnect()Disconnects from the server, clearing all subscriptions.
pause()Disconnects from the server without clearing subscriptions.

Managing subscriptions

NameDescription
subscribe(options, callback)Subscribes to a stream. Messages in this stream are passed to the callback function. See below for subscription options. Returns a Subscription object.
unsubscribe(Subscription)Unsubscribes the given Subscription.
unsubscribeAll(streamId)Unsubscribes all Subscriptions for streamId.
getSubscriptions(streamId)Returns a list of Subscriptions for streamId.

Stream API

All the below functions return a Promise which gets resolved with the result. They can also take an apiKey as an extra argument. Otherwise the apiKey defined in the StreamrClient options is used, if any.

NameDescription
getStream(streamId)Fetches a Stream object from the API.
listStreams(query)Fetches an array of Stream objects from the API. For the query params, consult the API docs.
getStreamByName(name)Fetches a Stream which exactly matches the given name.
createStream(properties)Creates a Stream with the given properties. For more information on the Stream properties, consult the API docs.
getOrCreateStream(properties)Gets a Stream with the id or name given in properties, or creates it if one is not found.
publish(streamId, message)Publishes a new message (data point) to the given Stream.

Listening to state changes of the client

on(eventName, function) | Binds a function to an event called eventName once(eventName, function) | Binds a function to an event called eventName. It gets called once and then removed. removeListener(eventName, function) | Unbinds the function from events called eventName

Stream object

All the below functions return a Promise which gets resolved with the result. They can also take an apiKey as an extra argument. Otherwise the apiKey defined in the StreamrClient options is used, if any.

NameDescription
update()Updates the properties of this Stream object by sending them to the API.
delete()Deletes this Stream.
getPermissions()Returns the list of permissions for this Stream.
detectFields()Updates the Stream field config (schema) to match the latest data point in the Stream.
publish(message)Publishes a new message (data point) to this Stream.

Subscription options

Note that only one of the resend options can be used for a particular subscription. The default functionality is to resend nothing, only subscribe to messages from the subscription moment onwards.

NameDescription
streamStream id to subscribe to
apiKeyUser key or stream key that authorizes the subscription. If defined, overrides the client's apiKey.
partitionPartition number to subscribe to. Defaults to the default partition (0).
resendObject defining the resend options. Below are examples of its contents.
// Resend N most recent messages
resend: {
    last: 10,
}

// Resend from a specific message reference up to the newest message
resend: {
    from: {
        timestamp: 12345,
        sequenceNumber: 0, // optional
    }
    publisher: 'publisherId', // optional
    msgChainId: 'msgChainId', // optional
}

// Resend a limited range of messages
resend: {
    from: {
        timestamp: 12345,
        sequenceNumber: 0, // optional
    },
    to: {
        timestamp: 54321,
        sequenceNumber: 0, // optional
    },
    publisher: 'publisherId', // optional
    msgChainId: 'msgChainId', // optional
}

Binding to events

The client and the subscriptions can fire events as detailed below. You can bind to them using on:

    // The StreamrClient emits various events
	client.on('connected', () => {
	    console.log('Yeah, we are connected now!')
	})

    // So does the Subscription object
	const sub = client.subscribe(...)
	sub.on('subscribed', () => {
	    console.log(`Subscribed to ${sub.streamId}`)
	})

Events on the StreamrClient instance

NameHandler ArgumentsDescription
connectedFired when the client has connected (or reconnected).
disconnectedFired when the client has disconnected (or paused).

Events on the Subscription object

NameHandler ArgumentsDescription
subscribed{ from: number }Fired when a subscription request is acknowledged by the server.
unsubscribedFired when an unsubscription is acknowledged by the server.
resendingFired when the subscription starts resending.
resentFired after resending when the subscription has finished resending.
no_resendFired after resending in case there was nothing to resend.
errorError objectReports errors, for example problems with message content

Logging

The Streamr JS client library supports debug for logging.

In node.js, start your app like this: DEBUG=StreamrClient* node your-app.js

In the browser, include debug.js and set localStorage.debug = 'StreamrClient'

0.0.1-tatum.6

7 months ago

0.0.1-tatum.7

6 months ago

0.0.1-tatum.8

6 months ago

0.0.1-tatum.5

7 months ago

0.0.1-tatum.3

7 months ago

0.0.1-tatum.4

7 months ago

0.0.1-tatum.1

8 months ago

0.0.1-tatum.2

8 months ago

0.0.1-tatum.0

8 months ago

8.5.4

10 months ago

8.5.3

11 months ago

8.5.5

10 months ago

8.5.2

11 months ago

8.5.1

11 months ago

8.5.5-nolit.1

10 months ago

7.3.0

1 year ago

8.1.0

1 year ago

7.4.0-beta.1

1 year ago

8.3.1

1 year ago

7.4.0-beta.0

1 year ago

8.2.1

1 year ago

8.2.0

1 year ago

7.3.0-beta.0

1 year ago

8.4.0

12 months ago

8.0.3-beta.0

1 year ago

8.3.0

1 year ago

8.5.0

12 months ago

8.0.0-beta.0

1 year ago

8.5.0-nolit.1

12 months ago

8.1.0-beta.0

1 year ago

8.5.0-nolit

12 months ago

8.0.4

1 year ago

8.3.0-beta.1

1 year ago

8.3.0-beta.0

1 year ago

5.6.0

1 year ago

8.0.1

1 year ago

8.0.0

1 year ago

8.0.3

1 year ago

8.0.2

1 year ago

7.0.0

1 year ago

7.0.3

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

7.1.0

1 year ago

7.2.1

1 year ago

7.2.0

1 year ago

7.0.0-beta.2

1 year ago

7.0.0-beta.3

1 year ago

7.0.0-beta.0

2 years ago

7.0.0-beta.1

1 year ago

6.0.10

2 years ago

6.1.0-beta.3

2 years ago

6.1.0-beta.4

2 years ago

6.1.0-beta.2

2 years ago

6.1.0-beta.1

2 years ago

6.0.7

2 years ago

6.0.6

2 years ago

6.0.9

2 years ago

6.0.8

2 years ago

6.0.3

2 years ago

6.0.5

2 years ago

6.0.4

2 years ago

6.0.2

2 years ago

6.0.0-alpha.21

2 years ago

6.0.0-alpha.22

2 years ago

6.0.0-beta.3

2 years ago

6.0.0-alpha.23

2 years ago

6.0.0-beta.4

2 years ago

6.0.0-alpha.24

2 years ago

6.0.0-alpha.25

2 years ago

6.0.0-alpha.26

2 years ago

6.0.0-alpha.27

2 years ago

6.0.0-alpha.28

2 years ago

6.0.0-beta.1

2 years ago

6.0.0-alpha.29

2 years ago

6.0.0-beta.2

2 years ago

6.0.0-alpha.30

2 years ago

6.0.0-alpha.31

2 years ago

6.0.0-alpha.32

2 years ago

6.0.0-alpha.33

2 years ago

5.5.8

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.0-alpha.18

2 years ago

6.0.0-alpha.19

2 years ago

5.5.7

2 years ago

5.5.6

2 years ago

5.5.5

2 years ago

5.5.4

2 years ago

5.5.3

2 years ago

5.5.2

2 years ago

5.5.1

2 years ago

5.5.0

2 years ago

6.0.0-alpha.7

3 years ago

6.0.0-alpha.8

3 years ago

6.0.0-alpha.9

3 years ago

6.0.0-alpha.10

3 years ago

6.0.0-alpha.11

3 years ago

6.0.0-alpha.12

3 years ago

6.0.0-alpha.13

3 years ago

6.0.0-alpha.14

3 years ago

6.0.0-alpha.15

3 years ago

6.0.0-alpha.16

3 years ago

6.0.0-alpha.17

3 years ago

5.4.5

3 years ago

6.0.0-alpha.5

3 years ago

6.0.0-alpha.6

3 years ago

6.0.0-alpha.4

3 years ago

6.0.0-alpha.3

3 years ago

6.0.0-alpha.2

3 years ago

6.0.0-alpha.0

3 years ago

6.0.0-alpha.1

3 years ago

5.4.4

3 years ago

5.4.3

3 years ago

5.4.1-alpha.1

3 years ago

5.4.1-alpha.2

3 years ago

5.4.2

3 years ago

5.3.0-alpha.3

3 years ago

5.4.1

3 years ago

5.4.0

3 years ago

5.3.0-beta.0

3 years ago

5.3.0-alpha.2

3 years ago

5.3.0-alpha.1

3 years ago

5.2.1

3 years ago

5.2.0-beta

3 years ago

5.1.0

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

5.0.0-beta.10

3 years ago

5.0.0-alpha.7

3 years ago

5.0.0-alpha.6

3 years ago

5.0.0-alpha.5

3 years ago

5.0.0-beta.8

3 years ago

5.0.0-beta.9

3 years ago

5.0.0-beta.7

3 years ago

5.0.0-beta.6

3 years ago

5.0.0-beta.5

3 years ago

5.0.0-beta.4

3 years ago

5.0.0-beta.3

3 years ago

4.1.6

3 years ago

4.1.5

3 years ago

5.0.0-beta.2

3 years ago

4.2.0-alpha.14

3 years ago

5.0.0-beta.1

3 years ago

4.2.0-alpha.13

3 years ago

4.1.4

3 years ago

5.0.0-alpha.4

3 years ago

4.2.0-alpha.12

3 years ago

5.0.0-alpha.3

3 years ago

5.0.0-alpha.2

3 years ago

5.0.0-alpha.1

3 years ago

4.2.0-alpha.11

3 years ago

4.2.0-alpha.10

3 years ago

4.2.0-alpha.9

3 years ago

4.2.0-alpha.8

3 years ago

4.2.0-alpha.7

3 years ago

4.1.3

4 years ago

4.2.0-alpha.6

4 years ago

4.2.0-alpha.5

4 years ago

4.1.2

4 years ago

4.2.0-alpha.4

4 years ago

4.2.0-alpha.3

4 years ago

4.2.0-alpha.2

4 years ago

4.2.0-alpha.1

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.2.0-alpha.0

4 years ago

4.1.0-beta.2

4 years ago

4.1.0-beta.1

4 years ago

4.1.0-beta.0

4 years ago

4.0.0-beta.2

4 years ago

4.0.0-beta.1

4 years ago

4.0.0-beta.0

4 years ago

3.2.1

4 years ago

3.2.1-beta.1

4 years ago

3.2.1-beta.0

4 years ago

3.2.0

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-beta.0

4 years ago

2.2.7

4 years ago

2.2.6

4 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.2.0-beta.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.1.0-beta.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-beta.9

5 years ago

2.0.0-beta.8

5 years ago

2.0.0-beta.7

5 years ago

2.0.0-beta.6

5 years ago

2.0.0-beta.5

5 years ago

2.0.0-beta.4

5 years ago

2.0.0-beta.3

5 years ago

1.0.2

5 years ago

2.0.0-beta.2

5 years ago

1.0.1

5 years ago

2.0.0-beta.1

5 years ago

1.0.0

5 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.11.0-beta.1

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.1

8 years ago

0.8.0

8 years ago