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

Commit ae24c67

Browse files
committed
Merge pull request #4 from streamr-dev/node-support
CORE-467 Node-friendliness
2 parents 9524f14 + ac05da1 commit ae24c67

File tree

6 files changed

+229
-82
lines changed

6 files changed

+229
-82
lines changed

README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
# streamr-client
22

3-
streamr-client is a JavaScript client for connecting to Streamr data. You can subscribe to user interface widget updates or even raw data streams.
3+
This is a JavaScript client for subscribing to realtime streams from [Streamr](http://www.streamr.com). Streamr is a realtime stream processing and analytics platform. This client allows you to write JS applications that leverage the stream computing and pub/sub features of Streamr. It works both in the browser and in node.js.
44

5-
## Requirements
5+
The streamr-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.
66

7-
* socket.io
7+
## Dependencies
8+
9+
* [socket.io-client](https://cdn.socket.io/socket.io-1.3.7.js)
10+
* [debug](https://github.com/visionmedia/debug) (optional)
11+
12+
In node.js, dependencies will be installed automatically with `npm install`. In the browser, make sure you include `socket.io-client` before `streamr-client`.
813

914
## Usage
1015

16+
The `examples` directory contains snippets for both browser and node.js.
17+
1118
```javascript
1219
client = new StreamrClient({
13-
// Connection options and default values
14-
server: 'data.streamr.com',
20+
// Connection options can be omitted, these are the default values
21+
server: 'https://data.streamr.com',
1522
autoConnect: true,
1623
autoDisconnect: true
1724
})
18-
client.subscribe(
25+
var sub = client.subscribe(
1926
'stream-id',
20-
function(message, streamId, counter) {
27+
function(message, streamId, timestamp, counter) {
2128
// Do something with the message, which is an object
2229
},
2330
{
@@ -29,7 +36,7 @@ client.connect()
2936

3037
## Handling messages
3138

32-
The second argument to client.subscribe() is the callback function that will be called for each message as they arrive. Its arguments are as follows:
39+
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:
3340

3441
Argument | Description
3542
-------- | -----------
@@ -75,16 +82,51 @@ getSubscriptions(`streamId`) | Returns a list of `Subscriptions` for `streamId`.
7582
bind(eventName, function) | Binds a `function` to an event called `eventName`
7683
unbind(eventName, function) | Unbinds the `function` from events called `eventName`
7784

78-
## Events on the client
85+
## Binding to events
86+
87+
The client and the subscriptions can fire events as detailed below. You can bind to them using `bind`:
88+
89+
```javascript
90+
function hello() {
91+
console.log('Hello!')
92+
}
93+
94+
client.bind('connected', hello)
95+
96+
var sub = client.subscribe(...)
97+
sub.bind('subscribed', function() {
98+
console.log('Subscribed to '+sub.streamId)
99+
})
100+
```
101+
102+
You can unbind using `unbind`:
103+
104+
```javascript
105+
client.unbind('connected', hello)
106+
```
107+
108+
109+
## Events on the StreamrClient instance
79110

80111
Name | Handler Arguments | Description
81112
---- | ----------------- | -----------
82113
connected | | Fired when the client has connected (or reconnected).
83114
disconnected | | Fired when the client has disconnected (or paused).
84115

85-
## Events on the `Subscription` object
116+
## Events on the Subscription object
86117

87118
Name | Handler Arguments | Description
88119
---- | ----------------- | -----------
89120
subscribed | {from: number} | Fired when a subscription request is acknowledged by the server.
90121
unsubscribed | | Fired when an unsubscription is acknowledged by the server.
122+
resending | | Fired when the subscription starts resending.
123+
resent | | Fired after `resending` when the subscription has finished resending.
124+
no_resend | | Fired after `resending` in case there was nothing to resend.
125+
126+
## Logging
127+
128+
This library supports the [debug](https://github.com/visionmedia/debug) library for logging.
129+
130+
In node.js, start your app like this: `DEBUG=StreamrClient node your-app.js`
131+
132+
In the browser, include `debug.js` and set `localStorage.debug = 'StreamrClient'`

examples/browser.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<html>
2+
<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>
5+
<script src="../streamr-client.js"></script>
6+
7+
<script>
8+
function log(msg) {
9+
var elem = document.createElement('p')
10+
elem.innerHTML = msg
11+
document.body.appendChild(elem)
12+
}
13+
14+
// Create the client with default options
15+
var client = new StreamrClient()
16+
// Subscribe to a stream
17+
var subscription = client.subscribe(
18+
'1ef8TbyGTFiAlZ8R2gCaJw',
19+
function(message) {
20+
// Handle the messages in this stream
21+
log(JSON.stringify(message))
22+
},
23+
{
24+
// Resend the last 10 messages on connect
25+
resend_last: 10
26+
}
27+
)
28+
29+
// Event binding examples
30+
client.bind('connected', function() {
31+
log('A connection has been established!')
32+
})
33+
34+
subscription.bind('subscribed', function() {
35+
log('Subscribed to '+subscription.streamId)
36+
})
37+
38+
subscription.bind('resending', function() {
39+
log('Resending from '+subscription.streamId)
40+
})
41+
42+
subscription.bind('resent', function() {
43+
log('Resend complete for '+subscription.streamId)
44+
})
45+
46+
subscription.bind('no_resend', function() {
47+
log('Nothing to resend for '+subscription.streamId)
48+
})
49+
</script>
50+
</head>
51+
<body>
52+
</body>
53+
</html>

examples/node.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// To enable debug logging:
2+
// DEBUG=StreamrClient node examples/node.js
3+
4+
// In your own app, use require('streamr-client') and get it from npm
5+
var StreamrClient = require('../streamr-client')
6+
7+
// Create the client with default options
8+
var client = new StreamrClient()
9+
// Subscribe to a stream
10+
var subscription = client.subscribe(
11+
'1ef8TbyGTFiAlZ8R2gCaJw',
12+
function(message) {
13+
// Handle the messages in this stream
14+
console.log(message)
15+
},
16+
{
17+
// Resend the last 10 messages on connect
18+
resend_last: 10
19+
}
20+
)
21+
22+
// Event binding examples
23+
client.bind('connected', function() {
24+
console.log('A connection has been established!')
25+
})
26+
27+
subscription.bind('subscribed', function() {
28+
console.log('Subscribed to '+subscription.streamId)
29+
})
30+
31+
subscription.bind('resending', function() {
32+
console.log('Resending from '+subscription.streamId)
33+
})
34+
35+
subscription.bind('resent', function() {
36+
console.log('Resend complete for '+subscription.streamId)
37+
})
38+
39+
subscription.bind('no_resend', function() {
40+
console.log('Nothing to resend for '+subscription.streamId)
41+
})

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"name": "streamr-client",
3-
"version": "0.0.1",
4-
"description": "",
3+
"version": "0.8.0",
4+
"description": "JS client for subscribing to Streamr streams",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/streamr-dev/streamr-client.git"
8+
},
59
"main": "streamr-client.js",
610
"directories": {
711
"test": "test"
@@ -12,10 +16,12 @@
1216
"author": "",
1317
"license": "",
1418
"dependencies": {
15-
19+
"socket.io-client": "1.3.7",
20+
"debug": "*"
1621
},
1722
"devDependencies": {
1823
"mocha": "*",
19-
"eventemitter2": "*"
24+
"eventemitter2": "*",
25+
"mockery": "*"
2026
}
2127
}

0 commit comments

Comments
 (0)