Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0'
Browse files Browse the repository at this point in the history
* release/v0.2.0:
  Update CHANGELOG
  Update Readme Update examples Remove useless dependency Update some dependencies
  Remove vendor split
  Add discard from Api and discard mine options Add ack handling for STOMP client
  • Loading branch information
gfoiani committed Mar 31, 2016
2 parents fda0b98 + a4e2e21 commit ec9be93
Show file tree
Hide file tree
Showing 38 changed files with 590 additions and 240 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
All notable changes to this project will be documented in this file.
`Space Bunny Node SDK` adheres to [Semantic Versioning](http://semver.org/).

- `0.1.x` Releases - [0.1.0](#210)
- `0.1.x` Releases - [0.1.0]
- `0.2.x` Releases - [0.2.0]

---

## [0.2.0](https://github.com/space-bunny/node-sdk/releases/tag/v0.2.0)

#### Added

- Ack / Nack handling for STOMP clients
- support for DiscardMine and DiscardFromApi options

## [0.1.0](https://github.com/space-bunny/node-sdk/releases/tag/v0.1.0)

#### Added
- AMQP, STOMP, MQTT clients
- AMQP, STOMP, MQTT stream clients

## [0.1.0](https://github.com/space-bunny/releases/tag/0.1.0)
80 changes: 76 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Please feel free to contribute!

`npm install spacebunny --save`

## Usage
## Basic usage

### Device

Expand Down Expand Up @@ -60,12 +60,16 @@ amqpClient.onReceive(messageCallback).then(function(res) {
});
```

For more advanced usage please refer to example files
For more advanced usage please refer to example files in `examples` folder

### Stream

A stream client can read from multiple live streams hooks
In this example the streamer receives all messages from stream `my-stream`

#### AMQP streamer

In this example the streamer receives all messages from stream `my-stream` and `my-stream-2`

```javascript
'use strict';
var AmqpStreamClient = require('spacebunny').AmqpStreamClient;
Expand All @@ -82,15 +86,83 @@ var streamClient = new AmqpStreamClient(connectionParams);
// Use your stream name
var streamHooks = [
{ stream: 'my-stream', callback: messageCallback },
{ stream: 'my-stream-2', callback: messageCallback }
];
streamClient.streamFrom(streamHooks).then(function(res) {
console.log(res);
}).catch(function(reason) {
console.error(reason);
});
```
For more advanced usage please refer to example files in `examples` folder

## Usage within a web page

Space Bunny Node SDK is bundled using Webpack to allow the integration of the library within a web page

### Device

#### STOMP receiver and publisher

In this example a device waits for incoming messages on its `inbox` channel and publishes a single message on the first configured channel

```html
<script src="https://raw.githubusercontent.com/space-bunny/node-sdk/master/dist/spacebunny.js"></script>
<script>
[...]
// Use your Api Key
var connectionParams = { apiKey: 'your-api-key' };
var webStompClient = new StompClient(connectionParams);
webStompClient.onReceive(messageCallback).then(function(res) {
console.log('Successfully connected!');
}).catch(function(reason) {
console.error(reason);
});
var content = { some: 'json' };
var channels = webStompClient.channels();
webStompClient.publish(channels[0], content).then(function(res) {
console.log('Message published!');
}).catch(function(reason) {
console.error(reason);
});
[...]
</script>
```

For more advanced usage please refer to example files in `dist` folder

### Stream

A stream client can read from multiple live streams hooks

#### STOMP streamer

In this example the streamer receives all messages from stream `my-stream` and `my-stream-2`

```html
<script src="https://raw.githubusercontent.com/space-bunny/node-sdk/master/dist/spacebunny.js"></script>
<script>
[...]
var connectionParams = {
client: 'your-client-id',
secret: 'your-secret'
};
var streamClient = new StompStreamClient(connectionParams);
// Use your stream name
var streamHooks = [
{ stream: 'my-stream', callback: messageCallback },
{ stream: 'my-stream-2', callback: messageCallback }
];
streamClient.streamFrom(streamHooks).then(function(res) {
console.log(res);
}).catch(function(reason) {
console.error(reason);
});
[...]
</script>
```

For more advanced usage please refer to example files
For more advanced usage please refer to example files in `dist` folder

## Watch changes to src files (with automatic transpilation on save)

Expand Down
6 changes: 4 additions & 2 deletions config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ exports.CONFIG = {
ssl: {
secureProtocol: 'TLSv1_method'
},
ackTypes: ['auto', 'manual'],
inputExchange: 'input',
fromApiHeader: 'x-from-sb-api',
protocol: 'amqp',
inboxTopic: 'inbox',
liveStreamSuffix: 'live_stream',
tempQueueSuffix: 'temp',
amqp: {
ackTypes: ['auto', 'manual'],
protocol: 'amqp',
ssl: {
protocol: 'amqps'
Expand All @@ -51,6 +51,7 @@ exports.CONFIG = {
}
},
stomp: {
ackTypes: ['client'],
connection: {
headers: {
max_hbrlck_fails: 10,
Expand All @@ -65,6 +66,7 @@ exports.CONFIG = {
}
},
webStomp: {
ackTypes: ['client'],
webSocket: {
protocol: 'ws',
ssl: {
Expand Down
5 changes: 2 additions & 3 deletions dist/device/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ <h3>Send a Message</h3>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script>
<!-- The SpaceBunny library -->
<script src="../vendors.js"></script>
<script src="../spacebunny.js"></script>
<script>
$(document).ready(function() {
Expand Down Expand Up @@ -105,8 +104,8 @@ <h3>Send a Message</h3>
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

var messageCallback = function(message) {
$('#messages').append('<pre>' + htmlEntities(message.body) + '</pre>');
var messageCallback = function(content, headers) {
$('#messages').append('<pre>' + htmlEntities(content) + '</pre>');
};
if(window.WebSocket) {

Expand Down
46 changes: 23 additions & 23 deletions dist/spacebunny.js

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions dist/stream/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ <h3>Topics</h3>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script>
<!-- The SpaceBunny library -->
<script src="../vendors.js"></script>
<script src="../spacebunny.js"></script>
<script>
$(document).ready(function() {
Expand Down Expand Up @@ -120,8 +119,8 @@ <h3>Topics</h3>
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

var messageCallback = function(message) {
$('#messages').append('<pre>' + htmlEntities(message.body) + '</pre>');
var messageCallback = function(content, headers) {
$('#messages').append('<pre>' + htmlEntities(content) + '</pre>');
};

var addTopic = function() {
Expand Down
1 change: 0 additions & 1 deletion dist/vendors.js

This file was deleted.

5 changes: 5 additions & 0 deletions docs/messages/amqpMessage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<a name="module_Message"></a>

## Message
A wrapper for the message object

5 changes: 5 additions & 0 deletions docs/messages/stompMessage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<a name="module_Message"></a>

## Message
A wrapper for the message object

22 changes: 18 additions & 4 deletions docs/spacebunny.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ A module that exports the base SpaceBunny client
* [SpaceBunny](#module_SpaceBunny)
* [~SpaceBunny](#module_SpaceBunny..SpaceBunny)
* [new SpaceBunny(opts)](#new_module_SpaceBunny..SpaceBunny_new)
* [.getConnectionParams()](#module_SpaceBunny..SpaceBunny+getConnectionParams)
* [.getEndpointConfigs()](#module_SpaceBunny..SpaceBunny+getEndpointConfigs)
* [.channels()](#module_SpaceBunny..SpaceBunny+channels)
* [.deviceId()](#module_SpaceBunny..SpaceBunny+deviceId)
* [.liveStreamByName(streamName)](#module_SpaceBunny..SpaceBunny+liveStreamByName)
* [.liveStreamExists(streamName)](#module_SpaceBunny..SpaceBunny+liveStreamExists)

<a name="module_SpaceBunny..SpaceBunny"></a>

Expand All @@ -19,10 +20,11 @@ A module that exports the base SpaceBunny client

* [~SpaceBunny](#module_SpaceBunny..SpaceBunny)
* [new SpaceBunny(opts)](#new_module_SpaceBunny..SpaceBunny_new)
* [.getConnectionParams()](#module_SpaceBunny..SpaceBunny+getConnectionParams)
* [.getEndpointConfigs()](#module_SpaceBunny..SpaceBunny+getEndpointConfigs)
* [.channels()](#module_SpaceBunny..SpaceBunny+channels)
* [.deviceId()](#module_SpaceBunny..SpaceBunny+deviceId)
* [.liveStreamByName(streamName)](#module_SpaceBunny..SpaceBunny+liveStreamByName)
* [.liveStreamExists(streamName)](#module_SpaceBunny..SpaceBunny+liveStreamExists)

<a name="new_module_SpaceBunny..SpaceBunny_new"></a>

Expand All @@ -32,9 +34,9 @@ A module that exports the base SpaceBunny client
| --- | --- | --- |
| opts | <code>Object</code> | constructor options may contain api-key or connection options |

<a name="module_SpaceBunny..SpaceBunny+getConnectionParams"></a>
<a name="module_SpaceBunny..SpaceBunny+getEndpointConfigs"></a>

#### spaceBunny.getConnectionParams() ⇒
#### spaceBunny.getEndpointConfigs() ⇒
Check if api-key or connection parameters have already been passed
If at least api-key is passed ask the endpoint for the configurations
else if also connection parameters are not passed raise an exception
Expand Down Expand Up @@ -63,3 +65,15 @@ Return a Stream ID from a stream name given in input
| --- | --- | --- |
| streamName | <code>String</code> | stream name |

<a name="module_SpaceBunny..SpaceBunny+liveStreamExists"></a>

#### spaceBunny.liveStreamExists(streamName) ⇒
Check if a stream exists

**Kind**: instance method of <code>[SpaceBunny](#module_SpaceBunny..SpaceBunny)</code>
**Returns**: true if stream exists, false otherwise

| Param | Type | Description |
| --- | --- | --- |
| streamName | <code>String</code> | stream name |

32 changes: 17 additions & 15 deletions examples/device/mqtt/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ var MqttClient = require('spacebunny').MqttClient;
// is not mandatory, but we'll use this for our example). You have also enabled 'data' channel for the device. See our
// Getting Started [link] for a quick introduction to Space Bunny's base concepts.

// If for some reason or use case it's not possible or desirable to use auto-configuration, Space Bunny's Ruby SDK
// permits to manually configure the connection with various methods.

// First of all go to Space Bunny's web interface, go to the devices section and create or pick an existing device.
// Click on the 'SHOW CONFIGURATION' link and, from the 'Full configuration' section, copy the required params
// Once everything is set up get your device's API key from Space Bunny's web application: on the web interface,
// go to devices section and create or pick an existing device. Click on the 'SHOW CONFIGURATION' link, copy the API key
// and substitute it here:
// You can also provide the endpointUrl to use a different end point, default is http://api.demo.spacebunny.io
var connectionParams = { apiKey: 'your-api-key' };

// Manual Config
// You can also provide full manual configuration
// var connectionParams = {
// deviceId: 'device-id',
// secret: 'device-secret',
// host: 'hostname',
// port: 1883, // default for MQTT
// vhost: 'vhost-id',
// port: 5672, // default for AMQP
// vhost: 'vhost',
// channels: [ 'data', 'alarms' ]
// };

// You can simply use device's api key to connect with default configurations
// Auto Config
var connectionParams = { apiKey: 'your-api-key' };

// Auto Config with SSL
// If you want to connecto using a secure channel, you must enable ssl
// and provide the client certificate path
// var connectionParams = {
// apiKey: 'your-api-key',
// ssl: true,
Expand All @@ -35,9 +32,14 @@ var connectionParams = { apiKey: 'your-api-key' };

var mqttClient = new MqttClient(connectionParams);

// Publishing Options
// retain: (default missing) if true means that messages are sent as retained messages
var publishingOpts = { retain: true };

var content = { some: 'json' };
// Get the first channel configured for the target device
var channel = mqttClient.channels()[0];
mqttClient.publish(channel, { some: 'json' }, { retain: true }).then(function(res) {
var channels = mqttClient.channels();
mqttClient.publish(channels[0], content, publishingOpts).then(function(res) {
console.log(res); // eslint-disable-line no-console
mqttClient.disconnect();
process.exit(0);
Expand Down
22 changes: 15 additions & 7 deletions examples/device/mqtt/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ var messageCallback = function(topic, message) {
console.log(topic + ':' + message); // eslint-disable-line no-console
};

// Manual Config
// Prerequisites: you have created a device through the Space Bunny's web interface. You also have a 'data' channel (name
// is not mandatory, but we'll use this for our example). You have also enabled 'data' channel for the device. See our
// Getting Started [link] for a quick introduction to Space Bunny's base concepts.

// Once everything is set up get your device's API key from Space Bunny's web application: on the web interface,
// go to devices section and create or pick an existing device. Click on the 'SHOW CONFIGURATION' link, copy the API key
// and substitute it here:
// You can also provide the endpointUrl to use a different end point, default is http://api.demo.spacebunny.io
var connectionParams = { apiKey: 'your-api-key' };

// You can also provide full manual configuration
// var connectionParams = {
// deviceId: 'device-id',
// secret: 'device-secret',
// host: 'hostname',
// port: 1883, // default for MQTT
// port: 5672, // default for AMQP
// vhost: 'vhost',
// channels: [ { name: 'data' }, { name: 'alarms' } ]
// channels: [ 'data', 'alarms' ]
// };

// Auto Config
var connectionParams = { apiKey: 'your-api-key' };

// Auto Config with SSL
// If you want to connecto using a secure channel, you must enable ssl
// and provide the client certificate path
// var connectionParams = {
// apiKey: 'your-api-key',
// ssl: true,
Expand Down
9 changes: 6 additions & 3 deletions examples/device/stomp/receiver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var StompClient = require('spacebunny').StompClient;

var messageCallback = function(message) {
console.log(message.body); // eslint-disable-line no-console
var messageCallback = function(content, headers) {
console.log(content); // eslint-disable-line no-console
};

// Prerequisites: you have created a device through the Space Bunny's web interface. You also have a 'data' channel (name
Expand Down Expand Up @@ -62,10 +62,13 @@ process.once('SIGINT', function() { disconnect(); });
// 'discardMine' (default false) causes the SDK to filter out auto-messages i.e. messages sent from this device
// and, for some reason, returned to the sender. This can happen in some particular situation such as when using m2m
// groups.

// 'ack' (default undefined) if ack option has value 'client' means that STOMP messages
// must be explicitly acked/nacked by client. Otherwise messages are automatically acked/nacked
var subscriptionOpts = { discardMine: false, discardFromApi: false };

// When a message is sent on the inbox channel of the current device, the callback function will bel called
stompClient.onReceive(messageCallback).then(function(res) {
stompClient.onReceive(messageCallback, subscriptionOpts).then(function(res) {
console.log(res); // eslint-disable-line no-console
}).catch(function(reason) {
console.error(reason); // eslint-disable-line no-console
Expand Down
Loading

0 comments on commit ec9be93

Please sign in to comment.