forked from resgateio/resgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleService.js
39 lines (34 loc) · 1.43 KB
/
exampleService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Connect to NATS server
const nats = require('nats').connect("nats://localhost:4222");
let model = { message: "Hello, secure World!" };
// Get listener. Reply with the json encoded model
nats.subscribe('get.example.model', function(req, reply) {
nats.publish(reply, JSON.stringify({ result: { model: model }}));
});
// Access listener. Only get access if the token is set with { foo:"bar" }
nats.subscribe('access.example.model', (req, reply) => {
let { token } = JSON.parse(req);
if (token && token.foo === 'bar') {
nats.publish(reply, JSON.stringify({ result: { get: true, call: "set" }}));
} else {
nats.publish(reply, JSON.stringify({ result: { get: false }}));
}
});
// Set listener for updating the model.message property
nats.subscribe('call.example.model.set', (req, reply) => {
let r = JSON.parse(req);
let p = r.params || {};
// Check if the message property was changed
if (typeof p.message === 'string' && p.message !== model.message) {
model.message = p.message;
// The model is updated. Send a change event.
nats.publish('event.example.model.change', JSON.stringify({
values: { message: p.message }
}));
}
// Reply success by sending an empty result
nats.publish(reply, JSON.stringify({ result: null }));
});
// System resets tells Resgate that the service has been (re)started.
// Resgate will then update any cached resource from example
nats.publish('system.reset', JSON.stringify({ resources: [ 'example.>' ] }));