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

Commit 437b4dc

Browse files
author
Eric Andrews
committed
Authentication support
1 parent 008b7f3 commit 437b4dc

File tree

3 files changed

+92
-84
lines changed

3 files changed

+92
-84
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
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 [socket.io](http://socket.io/) under the hood for streaming message delivery. It works in virtually all browsers by using websockets where available, or fallback methods on legacy browsers.
7+
The client uses [web sockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) under the hood for streaming message delivery.
88

99
### Installation
1010

@@ -14,7 +14,6 @@ The client is available on [npm](https://www.npmjs.com/package/streamr-client) a
1414

1515
### Dependencies
1616

17-
* [socket.io-client](https://cdn.socket.io/socket.io-1.3.7.js)
1817
* [debug](https://github.com/visionmedia/debug) (optional)
1918

2019
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.
@@ -35,6 +34,7 @@ var client = new StreamrClient({
3534
// Subscribe to a stream
3635
var sub = client.subscribe(
3736
'stream-id',
37+
'auth-key',
3838
function(message, streamId, timestamp, counter) {
3939
// Do something with a message, which is an object
4040
},
@@ -46,7 +46,7 @@ var sub = client.subscribe(
4646

4747
### Handling messages
4848

49-
The second argument to `client.subscribe(streamId, callback, resendOptions)` is the callback function that will be called for each message as they arrive. Its arguments are as follows:
49+
The third argument to `client.subscribe(streamId, authKey, callback, resendOptions)` is the callback function that will be called for each message as they arrive. Its arguments are as follows:
5050

5151
Argument | Description
5252
-------- | -----------
@@ -64,6 +64,7 @@ server | api.streamr.com | Address of the server to connect to.
6464
autoConnect | true | If set to `true`, the client connects automatically on the first call to `subscribe()`. Otherwise an explicit call to `connect()` is required.
6565
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()`.
6666
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.
67+
authKey | null | Define default authKey to use when none is specified in subscribe
6768

6869

6970
### Resend options
@@ -85,7 +86,7 @@ Name | Description
8586
connect() | Connects to the server, and also subscribes to any streams for which `subscribe()` has been called before calling `connect()`.
8687
disconnect() | Disconnects from the server, clearing all subscriptions.
8788
pause() | Disconnects from the server without clearing subscriptions.
88-
subscribe(streamId, callback, resendOptions) | Subscribes to a stream identified by the string `streamId`. Messages in this stream are passed to the `callback` function. See the above table for `resendOptions`. Returns a `Subscription` object.
89+
subscribe(streamId, authId, callback, resendOptions) | Subscribes to a stream identified by the string `streamId`. Authentication key `authId` is used. Messages in this stream are passed to the `callback` function. See the above table for `resendOptions`. Returns a `Subscription` object.
8990
unsubscribe(Subscription) | Unsubscribes the given `Subscription`.
9091
unsubscribeAll(`streamId`) | Unsubscribes all `Subscriptions` for `streamId`.
9192
getSubscriptions(`streamId`) | Returns a list of `Subscriptions` for `streamId`.

streamr-client.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@
480480
return id.toString()
481481
};
482482

483-
function Subscription(streamId, callback, options) {
483+
function Subscription(streamId, authKey, callback, options) {
484484
EventEmitter.call(this); // call parent constructor
485485

486486
if (!streamId)
@@ -492,6 +492,7 @@
492492

493493
this.id = generateSubscriptionId()
494494
this.streamId = streamId
495+
this.authKey = authKey
495496
this.callback = callback
496497
this.options = options || {}
497498
this.queue = []
@@ -679,7 +680,8 @@
679680
// Automatically connect on first subscribe
680681
autoConnect: true,
681682
// Automatically disconnect on last unsubscribe
682-
autoDisconnect: true
683+
autoDisconnect: true,
684+
authKey: null
683685
}
684686
this.subsByStream = {}
685687
this.subById = {}
@@ -720,7 +722,7 @@
720722
return this.subsByStream[streamId] || []
721723
}
722724

723-
StreamrClient.prototype.subscribe = function(streamId, callback, options) {
725+
StreamrClient.prototype.subscribe = function(streamId, authKey, callback, options) {
724726
var _this = this
725727

726728
if (!streamId)
@@ -732,9 +734,9 @@
732734
throw "subscribe: Invalid arguments: callback is required!"
733735

734736
// Create the Subscription object and bind handlers
735-
var sub = new Subscription(streamId, callback, options)
737+
var sub = new Subscription(streamId, authKey || this.authKey, callback, options)
736738
sub.on('gap', function(from, to) {
737-
_this._requestResend(sub, {resend_from: from, resend_to: to})
739+
_this._requestResend(sub, {resend_from: from, resend_to: to, authKey: sub.authKey })
738740
})
739741
sub.on('done', function() {
740742
debug("done event for sub %d", sub.id)
@@ -985,7 +987,7 @@
985987

986988
// If this is the first subscription for this stream, send a subscription request to the server
987989
if (!subs._subscribing && subscribedSubs.length === 0) {
988-
var req = extend({}, sub.options, {type: 'subscribe', channel: sub.streamId})
990+
var req = extend({}, sub.options, { type: 'subscribe', channel: sub.streamId, authKey: sub.authKey })
989991
debug("_requestSubscribe: subscribing client: %o", req)
990992
subs._subscribing = true
991993
_this.connection.send(req)
@@ -1021,7 +1023,12 @@
10211023

10221024
sub.resending = true
10231025

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

0 commit comments

Comments
 (0)