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

Commit a7072cf

Browse files
committed
change signature to subscribe(options, callback)
1 parent 965b01c commit a7072cf

File tree

5 files changed

+141
-104
lines changed

5 files changed

+141
-104
lines changed

README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,40 @@ Here's a quick example. More detailed examples for the browser and node.js can b
1919
```javascript
2020
// Create a StreamrClient instance
2121
var client = new StreamrClient({
22-
// See below for connection options
22+
// See below for options
2323
})
2424

2525
// Subscribe to a stream
2626
var sub = client.subscribe(
27-
'stream-id',
28-
'auth-key',
29-
function(message, streamId, timestamp, counter) {
30-
// Do something with a message, which is an object
31-
},
3227
{
33-
// Resend options, see below
28+
stream: 'streamId',
29+
partition: 0, // Optional, defaults to zero. Use for partitioned streams to select partition.
30+
authKey: 'authKey' // Optional. If not given, uses the authKey given at client creation time.
31+
// optional resend options here
32+
},
33+
function(message, metadata) {
34+
// Do something with the message, which is an object
3435
}
3536
)
3637
```
3738

38-
### Handling messages
39-
40-
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:
41-
42-
Argument | Description
43-
-------- | -----------
44-
message | A javascript object containing the message itself
45-
streamId | The id of the stream the message belongs to
46-
timestamp| (optional) A javascript Date object containing the timestamp for this message, if available.
47-
counter | (optional) A sequence number for this message, if available.
48-
49-
50-
### Connection options
39+
### Client options
5140

5241
Option | Default value | Description
5342
------ | ------------- | -----------
5443
url | ws://www.streamr.com/api/v1/ws | Address of the Streamr websocket endpoint to connect to.
5544
autoConnect | true | If set to `true`, the client connects automatically on the first call to `subscribe()`. Otherwise an explicit call to `connect()` is required.
5645
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()`.
57-
authKey | null | Define default authKey to use when none is specified in subscribe
46+
authKey | null | Define default authKey to use when none is specified in the call to `client.subscribe`.
47+
48+
### Message handler callback
49+
50+
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:
51+
52+
Argument | Description
53+
-------- | -----------
54+
message | A javascript object containing the message itself
55+
metadata | Metadata for the message, for example `metadata.timestamp` etc.
5856

5957
### Resend options
6058

examples/browser.html

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<html>
22
<head>
3-
<!-- For debug messages, include debug.js from https://github.com/visionmedia/debug and set localStorage.debug = 'StreamrClient' -->
4-
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
3+
<!-- For debug messages, include debug.js and set localStorage.debug = 'StreamrClient'. See from https://github.com/visionmedia/debug -->
54
<script src="../streamr-client.js"></script>
65

76
<script>
@@ -15,14 +14,14 @@
1514
var client = new StreamrClient()
1615
// Subscribe to a stream
1716
var subscription = client.subscribe(
18-
'1ef8TbyGTFiAlZ8R2gCaJw',
17+
{
18+
stream: '1ef8TbyGTFiAlZ8R2gCaJw',
19+
// Resend the last 10 messages on connect
20+
resend_last: 10
21+
},
1922
function(message) {
2023
// Handle the messages in this stream
2124
log(JSON.stringify(message))
22-
},
23-
{
24-
// Resend the last 10 messages on connect
25-
resend_last: 10
2625
}
2726
)
2827

examples/node.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ var StreamrClient = require('../streamr-client')
88
var client = new StreamrClient()
99
// Subscribe to a stream
1010
var subscription = client.subscribe(
11-
'1ef8TbyGTFiAlZ8R2gCaJw',
11+
{
12+
stream: '1ef8TbyGTFiAlZ8R2gCaJw',
13+
// Resend the last 10 messages on connect
14+
resend_last: 10
15+
},
1216
function(message) {
1317
// Handle the messages in this stream
1418
console.log(message)
15-
},
16-
{
17-
// Resend the last 10 messages on connect
18-
resend_last: 10
1919
}
2020
)
2121

streamr-client.js

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

483-
function Subscription(streamId, authKey, callback, options) {
483+
function Subscription(streamId, streamPartition, 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.streamPartition = streamPartition
495496
this.authKey = authKey
496497
this.callback = callback
497498
this.options = options || {}
@@ -608,7 +609,7 @@
608609
// Normal case where prevOffset == null || lastReceivedOffset == null || prevOffset === lastReceivedOffset
609610
else {
610611
this.lastReceivedOffset = offset
611-
this.callback(content, this.streamId, timestamp, offset)
612+
this.callback(content, msg)
612613
if (content[BYE_KEY]) {
613614
this.emit('done')
614615
}
@@ -722,21 +723,35 @@
722723
return this.subsByStream[streamId] || []
723724
}
724725

725-
StreamrClient.prototype.subscribe = function(streamId, authKey, callback, options) {
726+
StreamrClient.prototype.subscribe = function(options, callback, legacyOptions) {
726727
var _this = this
727728

728-
if (!streamId)
729-
throw "subscribe: Invalid arguments: stream id is required!"
730-
else if (typeof streamId !== 'string')
731-
throw "subscribe: stream id must be a string!"
732-
733-
if (!callback)
729+
if (!options) {
730+
throw "subscribe: Invalid arguments: subscription options is required!"
731+
} else if (!callback) {
734732
throw "subscribe: Invalid arguments: callback is required!"
733+
}
734+
735+
// Backwards compatibility for giving a streamId as first argument
736+
if (typeof options === 'string') {
737+
options = {
738+
stream: options
739+
}
740+
} else if (typeof options !== 'object') {
741+
throw "subscribe: options must be an object"
742+
}
743+
744+
// Backwards compatibility for giving an options object as third argument
745+
extend(options, legacyOptions)
746+
747+
if (!options.stream) {
748+
throw "subscribe: Invalid arguments: options.stream is not given"
749+
}
735750

736751
// Create the Subscription object and bind handlers
737-
var sub = new Subscription(streamId, authKey || this.options.authKey, callback, options)
752+
var sub = new Subscription(options.stream, options.partition || 0, options.authKey || this.options.authKey, callback, options)
738753
sub.on('gap', function(from, to) {
739-
_this._requestResend(sub, {resend_from: from, resend_to: to, authKey: sub.authKey })
754+
_this._requestResend(sub, {resend_from: from, resend_to: to})
740755
})
741756
sub.on('done', function() {
742757
debug("done event for sub %d", sub.id)
@@ -1023,7 +1038,7 @@
10231038

10241039
sub.resending = true
10251040

1026-
var request = extend({}, options, resendOptions, {type: 'resend', stream: sub.streamId, authKey: sub.authKey,sub: sub.id})
1041+
var request = extend({}, options, resendOptions, {type: 'resend', stream: sub.streamId, partition: sub.streamPartition, authKey: sub.authKey, sub: sub.id})
10271042
debug("_requestResend: %o", request)
10281043
this.connection.send(request)
10291044
}

0 commit comments

Comments
 (0)