-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from dowjones/feature/DNASNS-2660/nodejs-strea…
…m-client-maintenance [DNASNS-2660] NodeJS stream client maintenance
- Loading branch information
Showing
21 changed files
with
2,397 additions
and
5,152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ node_modules | |
/DowJonesDNA.json | ||
/r-d.sh | ||
/fix_git.sh | ||
|
||
.envrc | ||
.vscode/** | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# dj-dna-streaming-javascript | ||
|
||
DNA Streaming Client - written in Javascript. | ||
|
||
## How To Use | ||
|
||
#### Installing | ||
### Installing | ||
|
||
This project is an NPM module. That means it can be installed as a kind of library for your main project. To do this go to your main project's root. At the command line execute the following: | ||
|
||
|
@@ -13,28 +14,28 @@ npm install git+https://[email protected]/dowjones/dj-dna-streams-javascript.git -- | |
|
||
Alternatively you can simply check out this project from Git. | ||
|
||
#### Authentication | ||
### Authentication | ||
|
||
User Key | ||
|
||
#### Configuring The App | ||
### Configuring The App | ||
|
||
There are three ways to pass configuration variables to the app. Please note that environment variables (Option 1) will override values provided in the `customerConfig.json` file (Option 2). | ||
They will not override values passed directly to the `Listener` constructor (Option 3). | ||
|
||
Option 1. Set environment variables. | ||
#### Option 1. Set environment variables. | ||
|
||
###### User Key | ||
Export the following envirnment variables: | ||
|
||
**USER_KEY** | ||
Dow Jones provided user key. | ||
|
||
**SUBSCRIPTION_ID** | ||
This environment variable holds the subscription ID. | ||
|
||
Option 2. Modify the 'customerConfig.json' file. In this project's root you will find the 'customerConfig.json' file. Add your credentials and subscription ID. Ensure your additions follow the JSON data format conventions. | ||
This environment variable holds the subscription ID. | ||
|
||
###### User Key | ||
#### Option 2. Modifying `customerConfig.json` | ||
|
||
In this project's root you will find the `customerConfig.json` file. Add your credentials and subscription ID. Ensure your additions follow the JSON data format conventions. | ||
|
||
``` | ||
{ | ||
|
@@ -43,14 +44,14 @@ Option 2. Modify the 'customerConfig.json' file. In this project's root you will | |
} | ||
``` | ||
|
||
or | ||
#### Option 3. Passing values as function arguments. | ||
|
||
Option 3: Passing values as function arguments. Specifically you can pass either the service account credentials and/or subscription ID. When you start a listener you can pass the service account crendentials to the Listener constructor as an object with the field "user_key", like so: | ||
Specifically you can pass either the service account credentials and/or subscription ID. When you start a listener you can pass the service account crendentials to the Listener constructor as an object with the field "user_key", like so: | ||
|
||
~~~~ | ||
var Listener = require('dj-dna-streaming-javascript').Listener; | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
var onMessageCallback = function(msg) { | ||
const onMessageCallback = async function(msg) { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
}; | ||
|
@@ -68,28 +69,28 @@ This will override both the environment variable and the configuration file serv | |
If you want to pass the subscription ID via function arguments, take a look at the following code: | ||
|
||
~~~~ | ||
var Listener = require('dj-dna-streaming-javascript').Listener; | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
var onMessageCallback = function(msg) { | ||
const onMessageCallback = async function(msg) { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
}; | ||
var subscriptionId = 'abcdefghi123'; | ||
const subscriptionId = 'abcdefghi123'; | ||
const listener = new Listener(); | ||
listener.listen(onMessageCallback, subscriptionId); | ||
~~~~ | ||
|
||
|
||
#### Running the Demo Code | ||
### Running the Demo Code | ||
|
||
This modules comes with demonstration code. To execute the demo code, configure your app (See _Configuring the App_ section above). Then execute the following: | ||
|
||
~~~ | ||
npm run demo | ||
~~~ | ||
|
||
##### Docker Demo | ||
### Docker Demo | ||
|
||
To execute the demo code in a Docker container, perform the following steps. | ||
|
||
|
@@ -101,58 +102,148 @@ Step 1: Build the docker image. Execute the following command line: | |
|
||
Step 2: Run the docker image | ||
|
||
###### User Key | ||
|
||
~~~ | ||
docker run -it \ | ||
-e USER_KEY="<your user key>" \ | ||
-e SUBSCRIPTION_ID="<your subscription ID>" \ | ||
dj-dna-streaming-javascript | ||
~~~ | ||
|
||
###### Client Credentials | ||
~~~ | ||
docker run -it \ | ||
-e USER_ID="<your user ID>" \ | ||
-e CLIENT_ID="<your client ID>" \ | ||
-e PASSWORD="<your password>" \ | ||
-e SUBSCRIPTION_ID="<your subscription ID>" \ | ||
dj-dna-streaming-javascript | ||
~~~ | ||
## Writing Your Own Code | ||
|
||
The following is some very basic code. Use it to listen to a DNA subscription. It assumes you have correctly configured the app. (See the *Configuring The App* section above). | ||
|
||
You can use two patterns to consume the messages from the subscription. The first option is using an async function or function returning a Promise or theanable: | ||
|
||
#### Writing Your Own Code | ||
### Async Function, Promise or Theanable | ||
|
||
The following is some very basic code. Use it to listen to a DNA subscription. It assumes you have configured the app correct. (See the *Configuring The App* section above). | ||
Write an async function or function returning a Promise/Theanable processing the messages. When the | ||
promise is resolved the message is acknowledged, in case the promise is rejected the message will be | ||
not acknowledged, so it can be processed again. | ||
|
||
~~~~ | ||
var Listener = require('dj-dna-streaming-javascript').Listener; | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
var onMessageCallback = function(msg) { | ||
const onMessageCallback = async function(msg) { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
}; | ||
const listener = new Listener(); | ||
listener.listen(onMessageCallback); | ||
~~~~ | ||
|
||
###### Error Handling | ||
### Callback pattern | ||
|
||
If your callback fails, the message will be nack'd and the listener will rethrow the error. If you wish to write your own error handling for callbacks then set the `userErrorHandling` parameter to true. This allows you to use an error handler callback to force the callback handler to nack messages. The following is a very basic example illustrating how this may work. | ||
Write a callback function with two parameters, the first one being the message. The second one is a function which must be called with null as a parameter if the message is correctly processed or an error. If the parameter is null when calling handleErr, the message will be acknowledged, if not, the message will be not acknowledged. | ||
|
||
~~~~ | ||
var Listener = require('dj-dna-streaming-javascript').Listener; | ||
var onMessageCallback = function((msg, handleErr) { | ||
let err = null; | ||
try { | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
const onMessageCallback = (msg, handleErr) => { | ||
let err = null; | ||
try { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
} catch (e) { | ||
err = e | ||
}; | ||
} catch (e) { | ||
err = e | ||
}; | ||
handleErr(err) | ||
}; | ||
const listener = new Listener(); | ||
listener.listen(onMessageCallback, null, false); | ||
~~~~ | ||
|
||
### Shutting down the listener | ||
|
||
The listener have a method to stop receiving new messages. After a set time you can call this method or you can add a termination signal listener to call it and close cleanly the listener. | ||
|
||
#### Waiting a set time | ||
|
||
~~~~ | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
const onMessageCallback = async function(msg) { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
}; | ||
const listener = new Listener(); | ||
listener.listen(onMessageCallback, my_subscription_id, true); | ||
listener.listen(onMessageCallback); | ||
// Listen to messages for 10 seconds | ||
setTimeout(() => { | ||
listener.closeListener(); | ||
}, 10000); | ||
~~~~ | ||
|
||
#### Ading a listener to the SIGTERM or SIGINT signals | ||
|
||
~~~~ | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
const onMessageCallback = async function(msg) { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
}; | ||
const listener = new Listener(); | ||
listener.listen(onMessageCallback); | ||
// This method calls the listener's closeListener method. | ||
const terminationHandler = () => { | ||
listener.closeListener(); | ||
}; | ||
// Adding this method as a handler of SIGTERM or SIGINT | ||
process.on('SIGINT', terminationHandler); | ||
process.on('SIGTERM', terminationHandler); | ||
~~~~ | ||
|
||
### Migrating from a synchronous callback function | ||
|
||
The latest version of the client require the callback function to conform to the callback pattern or using a function returning a Promise or Theanable. | ||
|
||
When having a synchronous callback function as the following: | ||
~~~~~ | ||
const oldSynchronousCallback = (msg) => { | ||
console.log('One incoming message:' + JSON.stringify(msg.data)); | ||
} | ||
~~~~~ | ||
|
||
#### Create Promise from the old callback function: | ||
|
||
~~~~ | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
const newAsyncCallback = (msg) => { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
resolve(oldSynchronousCallback(msg)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
const listener = new Listener(); | ||
listener.listen(newAsyncCallback); | ||
~~~~ | ||
|
||
#### Use the callback pattern: | ||
~~~~ | ||
const Listener = require('dj-dna-streaming-javascript').Listener; | ||
const onMessageCallback = (msg, handleErr) => { | ||
let err = null; | ||
try { | ||
oldSynchronousCallback(msg); | ||
} catch (e) { | ||
err = e | ||
}; | ||
handleErr(err) | ||
}; | ||
const listener = new Listener(); | ||
listener.listen(onMessageCallback, null, false); | ||
~~~~ |
Oops, something went wrong.