@ably/delta-codec
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@ably/delta-codec", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Utility library supporting those consuming delta streams from Ably without using the official JavaScript client library (e.g. MQTT and SSE applications).", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
119
README.md
@@ -22,3 +22,3 @@ # Vcdiff Codec Library for JavaScript | ||
```javascript | ||
```js | ||
var deltaCodec = require('@ably/delta-codec'); | ||
@@ -89,3 +89,3 @@ ``` | ||
### Text stream from Ably via SSE (enveloped) | ||
### Node.js: Text stream from Ably via SSE (enveloped) | ||
@@ -96,22 +96,30 @@ By default the event data received from Ably is enveloped in JSON format. | ||
```js | ||
const url = 'https://realtime.ably.io/sse?v=1.1&key=' + API_KEY + '&channels=' + CHANNEL_NAME; | ||
const deltaCodec = require('@ably/delta-codec'); | ||
const EventSource = require('eventsource'); | ||
const prefix = '[?delta=vcdiff]'; | ||
const url = `https://realtime.ably.io/event-stream?channels=${prefix}${CHANNEL_NAME}&v=1.2&key=${APP_KEY}`; | ||
const eventSource = new EventSource(url); | ||
const decoder = new DeltaCodec.VcdiffDecoder(); | ||
eventSource.onmessage = (event) => { | ||
var message = JSON.parse(event.data); | ||
let stringData = message.data; | ||
try { | ||
if (message.extras.delta) { | ||
stringData = decoder.applyBase64Delta(stringData).asUtf8String(); | ||
} else { | ||
decoder.setBase(stringData); | ||
} | ||
} catch(e) { | ||
console.log(e); // TODO: Handle error. | ||
const decoder = new deltaCodec.CheckedVcdiffDecoder(); | ||
eventSource.onmessage = function onEventSourceMessage(event) { | ||
const message = JSON.parse(event.data); | ||
let value; | ||
const deltaExtras = (message.extras && message.extras.delta) ? message.extras.delta : null; | ||
if (deltaExtras) { | ||
if (deltaExtras.format !== 'vcdiff') { | ||
throw new Error(`Delta format ${deltaExtras.format} not understood.`); | ||
} | ||
console.log(data); // TODO: Process the received value. | ||
value = decoder.applyBase64Delta(message.data, message.id, deltaExtras.from).asUtf8String(); | ||
} else { | ||
value = message.data; | ||
decoder.setBase(value, message.id); | ||
} | ||
console.log(`received: ${value}`); | ||
}; | ||
eventSource.onerror = function onEventSourceError(event) { | ||
console.log(`error: ${event.data}`); | ||
}; | ||
``` | ||
### Text stream from Ably via SSE (not enveloped) | ||
### Node.js: Text stream from Ably via SSE (not enveloped) | ||
@@ -124,42 +132,56 @@ For this example we have subscribed to Ably as our event source and specified that we do not want the inbound event data to be JSON enveloped. | ||
```js | ||
const url = 'https://realtime.ably.io/sse?v=1.1&key=' + API_KEY + '&channels=' + CHANNEL_NAME + '&enveloped=false'; | ||
const deltaCodec = require('@ably/delta-codec'); | ||
const EventSource = require('eventsource'); | ||
const prefix = '[?delta=vcdiff]'; | ||
const url = `https://realtime.ably.io/event-stream?channels=${prefix}${CHANNEL_NAME}&v=1.2&key=${APP_KEY}&enveloped=false`; | ||
const eventSource = new EventSource(url); | ||
const decoder = new DeltaCodec.VcdiffDecoder(); | ||
eventSource.onmessage = (event) => { | ||
let stringData = event.data; | ||
try { | ||
if (VcdiffDecoder.isBase64Delta(stringData)) { | ||
stringData = decoder.applyBase64Delta(stringData).asUtf8String(); | ||
} else { | ||
decoder.setBase(stringData); | ||
} | ||
} catch(e) { | ||
console.log(e); // TODO: Handle error. | ||
} | ||
console.log(data); // TODO: Process the received value. | ||
const decoder = new deltaCodec.VcdiffDecoder(); | ||
eventSource.onmessage = function onEventSourceMessage(event) { | ||
const stringData = event.data; | ||
let value; | ||
if (deltaCodec.VcdiffDecoder.isBase64Delta(stringData)) { | ||
value = decoder.applyBase64Delta(stringData).asUtf8String(); | ||
} else { | ||
value = stringData; | ||
decoder.setBase(value); | ||
} | ||
console.log(`received: ${value}`); | ||
}; | ||
eventSource.onerror = function onEventSourceError(event) { | ||
console.log(`error: ${event.data}`); | ||
}; | ||
``` | ||
### Binary stream from Ably via MQTT | ||
### Node.js: Binary stream from Ably via MQTT | ||
The raw binary data received over MQTT has no encoding or other form of envelope encapsulating it. | ||
We need to 'sniff' each inbound payload to identify whether it is an absolute value or a delta to be applied to the previously received value. | ||
In this example we are transporting UTF-8 encoded strings. | ||
```js | ||
const client = mqtt.connect(url, options); | ||
const channelName = 'sample-app-mqtt'; | ||
const decoder = new VcdiffDecoder(); | ||
client.on('message', (_, payload) => { | ||
let data = payload; | ||
try { | ||
if (VcdiffDecoder.isDelta(data)) { | ||
data = decoder.applyDelta(data).asUint8Array(); | ||
} else { | ||
decoder.setBase(data); | ||
} | ||
} catch(e) { | ||
console.log(e); // TODO: Handle error. | ||
} | ||
console.log(data); // TODO: Process the received value. | ||
const deltaCodec = require('@ably/delta-codec'); | ||
const mqtt = require('mqtt'); | ||
const brokerUrl = `mqtts://mqtt.ably.io`; | ||
const options = { | ||
username: APP_KEY_NAME, | ||
password: APP_KEY_SECRET, | ||
}; | ||
const prefix = '[?delta=vcdiff]'; | ||
const client = mqtt.connect(brokerUrl, options); | ||
client.on('connect', () => { | ||
client.subscribe(`${prefix}${CHANNEL_NAME}`); | ||
}); | ||
const decoder = new deltaCodec.VcdiffDecoder(); | ||
client.on('message', (topic, message) => { | ||
let value; | ||
if (deltaCodec.VcdiffDecoder.isDelta(message)) { | ||
value = decoder.applyDelta(message).asUtf8String(); | ||
} else { | ||
decoder.setBase(message); | ||
value = message.toString(); | ||
} | ||
console.log(`received: ${value}`); | ||
}); | ||
``` | ||
@@ -204,3 +226,4 @@ | ||
2. Release the tagged commit to Github using `git push origin master --follow-tags` | ||
3. Release to NPM using `npm publish . --access public` | ||
3. Release to NPM using `npm publish . --access public` ([this package](https://www.npmjs.com/package/@ably/delta-codec) is configured to require that [2FA](https://docs.npmjs.com/configuring-two-factor-authentication) is used by publishers) | ||
4. Release to Ably's CDN using `npm run grunt -- publish-cdn` (operable by Ably staff only) | ||
5. Visit [tags](https://github.com/ably/delta-codec-js/tags) and draft new release for the newly created tag |
263801
225