You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Alternatively you can simply check out this project from Git.
15
16
16
-
####Authentication
17
+
### Authentication
17
18
18
19
User Key
19
20
20
-
####Configuring The App
21
+
### Configuring The App
21
22
22
23
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).
23
24
They will not override values passed directly to the `Listener` constructor (Option 3).
24
25
25
-
Option 1. Set environment variables.
26
+
#### Option 1. Set environment variables.
26
27
27
-
###### User Key
28
+
Export the following envirnment variables:
28
29
29
30
**USER_KEY**
30
31
Dow Jones provided user key.
31
32
32
33
**SUBSCRIPTION_ID**
33
-
This environment variable holds the subscription ID.
34
-
35
-
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.
34
+
This environment variable holds the subscription ID.
36
35
37
-
###### User Key
36
+
#### Option 2. Modifying `customerConfig.json`
37
+
38
+
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.
38
39
39
40
```
40
41
{
@@ -43,14 +44,14 @@ Option 2. Modify the 'customerConfig.json' file. In this project's root you will
43
44
}
44
45
```
45
46
46
-
or
47
+
#### Option 3. Passing values as function arguments.
47
48
48
-
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:
49
+
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:
49
50
50
51
~~~~
51
-
var Listener = require('dj-dna-streaming-javascript').Listener;
This modules comes with demonstration code. To execute the demo code, configure your app (See _Configuring the App_ section above). Then execute the following:
87
88
88
89
~~~
89
90
npm run demo
90
91
~~~
91
92
92
-
#####Docker Demo
93
+
### Docker Demo
93
94
94
95
To execute the demo code in a Docker container, perform the following steps.
95
96
@@ -101,58 +102,148 @@ Step 1: Build the docker image. Execute the following command line:
101
102
102
103
Step 2: Run the docker image
103
104
104
-
###### User Key
105
-
106
105
~~~
107
106
docker run -it \
108
107
-e USER_KEY="<your user key>" \
109
108
-e SUBSCRIPTION_ID="<your subscription ID>" \
110
109
dj-dna-streaming-javascript
111
110
~~~
112
111
113
-
###### Client Credentials
114
-
~~~
115
-
docker run -it \
116
-
-e USER_ID="<your user ID>" \
117
-
-e CLIENT_ID="<your client ID>" \
118
-
-e PASSWORD="<your password>" \
119
-
-e SUBSCRIPTION_ID="<your subscription ID>" \
120
-
dj-dna-streaming-javascript
121
-
~~~
112
+
## Writing Your Own Code
113
+
114
+
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).
122
115
116
+
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:
123
117
124
-
#### Writing Your Own Code
118
+
###Async Function, Promise or Theanable
125
119
126
-
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).
120
+
Write an async function or function returning a Promise/Theanable processing the messages. When the
121
+
promise is resolved the message is acknowledged, in case the promise is rejected the message will be
122
+
not acknowledged, so it can be processed again.
127
123
128
124
~~~~
129
-
var Listener = require('dj-dna-streaming-javascript').Listener;
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.
137
+
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.
142
138
143
139
~~~~
144
-
var Listener = require('dj-dna-streaming-javascript').Listener;
145
-
146
-
var onMessageCallback = function((msg, handleErr) {
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.
// This method calls the listener's closeListener method.
193
+
const terminationHandler = () => {
194
+
listener.closeListener();
195
+
};
196
+
197
+
// Adding this method as a handler of SIGTERM or SIGINT
198
+
process.on('SIGINT', terminationHandler);
199
+
process.on('SIGTERM', terminationHandler);
200
+
~~~~
201
+
202
+
### Migrating from a synchronous callback function
203
+
204
+
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.
205
+
206
+
When having a synchronous callback function as the following:
0 commit comments