Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 965b01c

Browse files
author
Eric Andrews
committed
Merge remote-tracking branch 'origin/master' into stream-auth
2 parents 83b0dcd + 8a7665e commit 965b01c

File tree

3 files changed

+78
-94
lines changed

3 files changed

+78
-94
lines changed

README.md

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
<!-- Note that this readme is embedded on API Documentation page within Streamr. Please don't use first-level headings (h1). You should write this document so that it will work both as a stand-alone document in the public GitHub repo and as a section in the API docs. -->
22
<a name="js-client"></a>
3-
## Streamr JavaScript Client
3+
## Streamr Javascript Client
44

5-
By using this client, you can easily subscribe to realtime [Streamr](http://www.streamr.com) streams from JavaScript-based environments, such as browsers and [node.js](https://nodejs.org). This enables you to use Streamr as an over-the-internet pub/sub engine with powerful analytics and automation features.
5+
By using this client, you can easily subscribe to realtime [Streamr](http://www.streamr.com) streams from Javascript-based environments, such as browsers and [node.js](https://nodejs.org). This enables you to use Streamr as an over-the-internet pub/sub engine with powerful analytics and automation features.
66

7-
The client uses [web sockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) under the hood for streaming message delivery.
7+
The client uses websockets for streaming message delivery. It should work in all modern browsers.
88

99
### Installation
1010

1111
The client is available on [npm](https://www.npmjs.com/package/streamr-client) and can be installed simpy by:
1212

1313
`npm install streamr-client`
1414

15-
### Dependencies
16-
17-
* [debug](https://github.com/visionmedia/debug) (optional)
18-
19-
In node.js, dependencies will be installed automatically with `npm install`. In the browser, make sure you include `socket.io-client` before `streamr-client` in your HTML.
20-
2115
### Usage
2216

2317
Here's a quick example. More detailed examples for the browser and node.js can be found [here](https://github.com/streamr-dev/streamr-client/tree/master/examples).
2418

2519
```javascript
2620
// Create a StreamrClient instance
2721
var client = new StreamrClient({
28-
// Connection options can be omitted, these are the default values
29-
server: 'https://data.streamr.com',
30-
autoConnect: true,
31-
autoDisconnect: true
22+
// See below for connection options
3223
})
3324

3425
// Subscribe to a stream
@@ -60,20 +51,18 @@ counter | (optional) A sequence number for this message, if available.
6051

6152
Option | Default value | Description
6253
------ | ------------- | -----------
63-
server | api.streamr.com | Address of the server to connect to.
54+
url | ws://www.streamr.com/api/v1/ws | Address of the Streamr websocket endpoint to connect to.
6455
autoConnect | true | If set to `true`, the client connects automatically on the first call to `subscribe()`. Otherwise an explicit call to `connect()` is required.
65-
autoDisconnect | true  | If set to `true`, the client automatically disconnects when the last channel is unsubscribed. Otherwise the connection is left open and can be disconnected explicitly by calling `disconnect()`.
66-
transports | null | Override default transport selection / upgrade scheme. For example, value `["websocket"]` will force use of sockets right from the beginning, while value `["polling"]` will allow only long-polling to be used.
56+
autoDisconnect | true  | 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()`.
6757
authKey | null | Define default authKey to use when none is specified in subscribe
6858

69-
7059
### Resend options
7160

7261
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.
7362

7463
Option | Default value | Description
7564
------ | ------------- | -----------
76-
resend_all | undefined | Set to `true` if you want all the messages for the channel resent from the earliest available message.
65+
resend_all | undefined | Set to `true` if you want all the messages for the stream resent from the earliest available message.
7766
resend_last | undefined | Resend the previous `N` messages.
7867
resend_from | undefined | Resend from a specific message number.
7968
resend_from_time | undefined | Resend from a specific Date (or millisecond timestamp).
@@ -136,7 +125,7 @@ no_resend | | Fired after `resending` in case there was nothing to resend.
136125

137126
### Logging
138127

139-
The Streamr JavaScript client library supports [debug](https://github.com/visionmedia/debug) for logging.
128+
The Streamr JS client library supports [debug](https://github.com/visionmedia/debug) for logging.
140129

141130
In node.js, start your app like this: `DEBUG=StreamrClient node your-app.js`
142131

streamr-client.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@
390390

391391
createSubscribeRequest: function(stream, resendOptions) {
392392
var req = {
393-
channel: stream
393+
stream: stream
394394
}
395395
Object.keys(resendOptions).forEach(function(key) {
396396
req[key] = resendOptions[key]
@@ -405,7 +405,7 @@
405405
function Connection(options) {
406406
EventEmitter.call(this);
407407
if (!options.url) {
408-
throw "Server is not defined!"
408+
throw "URL is not defined!"
409409
}
410410
this.options = options
411411
this.connected = false
@@ -842,10 +842,10 @@
842842

843843
this.connection.on('subscribed', function(response) {
844844
if (response.error) {
845-
_this.handleError("Error subscribing to "+response.channel+": "+response.error)
845+
_this.handleError("Error subscribing to "+response.stream+": "+response.error)
846846
}
847847
else {
848-
var subs = _this.subsByStream[response.channel]
848+
var subs = _this.subsByStream[response.stream]
849849
delete subs._subscribing
850850

851851
debug('Client subscribed: %o', response)
@@ -862,9 +862,9 @@
862862
this.connection.on('unsubscribed', function(response) {
863863
debug("Client unsubscribed: %o", response)
864864

865-
if (_this.subsByStream[response.channel]) {
865+
if (_this.subsByStream[response.stream]) {
866866
// Copy the list to avoid concurrent modifications
867-
var l = _this.subsByStream[response.channel].slice()
867+
var l = _this.subsByStream[response.stream].slice()
868868
l.forEach(function(sub) {
869869
_this._removeSubscription(sub)
870870
sub.emit('unsubscribed')
@@ -954,9 +954,9 @@
954954
}
955955

956956
StreamrClient.prototype._checkAutoDisconnect = function() {
957-
// Disconnect if no longer subscribed to any channels
957+
// Disconnect if no longer subscribed to any streams
958958
if (this.options.autoDisconnect && Object.keys(this.subsByStream).length === 0) {
959-
debug("Disconnecting due to no longer being subscribed to any channels")
959+
debug("Disconnecting due to no longer being subscribed to any streams")
960960
this.disconnect()
961961
}
962962
}
@@ -987,7 +987,7 @@
987987

988988
// If this is the first subscription for this stream, send a subscription request to the server
989989
if (!subs._subscribing && subscribedSubs.length === 0) {
990-
var req = extend({}, sub.options, { type: 'subscribe', channel: sub.streamId, authKey: sub.authKey })
990+
var req = extend({}, sub.options, { type: 'subscribe', stream: sub.streamId, authKey: sub.authKey })
991991
debug("_requestSubscribe: subscribing client: %o", req)
992992
subs._subscribing = true
993993
_this.connection.send(req)
@@ -1006,7 +1006,7 @@
10061006
debug("Client unsubscribing stream %o", streamId)
10071007
this.connection.send({
10081008
type: 'unsubscribe',
1009-
channel: streamId
1009+
stream: streamId
10101010
})
10111011
}
10121012

@@ -1023,12 +1023,7 @@
10231023

10241024
sub.resending = true
10251025

1026-
var request = extend({}, options, resendOptions, {
1027-
type: 'resend',
1028-
channel: sub.streamId,
1029-
authKey: sub.authKey,
1030-
sub: sub.id
1031-
})
1026+
var request = extend({}, options, resendOptions, {type: 'resend', stream: sub.streamId, authKey: sub.authKey,sub: sub.id})
10321027
debug("_requestResend: %o", request)
10331028
this.connection.send(request)
10341029
}

0 commit comments

Comments
 (0)