Skip to content

Commit

Permalink
feat: notifiers support in aries rest client js worker
Browse files Browse the repository at this point in the history
- new constructor opts `agent-rest-wshook` in rest client based aries js
worker. If provided then `aries.startNotifier` can be used to listen to
incoming messages

- closes hyperledger-archives#1370

Signed-off-by: sudesh.shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Feb 26, 2020
1 parent 8e27bb3 commit e4d0b76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/aries-js-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ Then initialize your aries instance:
```js
const aries = await new Aries.Framework({
assetsPath: "/path/serving/the/assets", // still required for assets other than the wasm
"agent-rest-url": "http://controller.api.example.com"
"agent-rest-url": "http://controller.api.example.com", // REST controller URL of the agent
"agent-rest-wshook": "ws://controller.api.example.com" // Optional REST controller websocket URL from which you can listen to notifications
})
```

Expand Down
2 changes: 1 addition & 1 deletion cmd/aries-js-worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
document.getElementById("mode-label").innerHTML = `Running in ${mode} mode`

if (src == "rest"){
document.getElementById("opts").innerHTML = `{"assetsPath": "/dist/assets", "agent-rest-url": "http://localhost:8082"}`
document.getElementById("opts").innerHTML = `{"assetsPath": "/dist/assets", "agent-rest-url": "http://localhost:8082", "agent-rest-wshook":"ws://localhost:8881"}`
} else {
document.getElementById("opts").innerHTML = `{"assetsPath": "/dist/assets", "agent-default-label":"dem-js-agent","http-resolver-url":[],"auto-accept":true,"outbound-transport":["ws","http"],"transport-return-route":"all","log-level":"debug"}`
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/aries-js-worker/src/aries.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function newMsg(pkg, fn, payload) {
* "outbound-transport": ["ws", "http"],
* "transport-return-route": "all",
* "log-level": "debug",
* "agent-rest-url": "http://controller.api.example.com"
* "agent-rest-url": "http://controller.api.example.com",
* "agent-rest-wshook": "ws://controller.api.example.com"
* }
*
* @param opts framework initialization options.
Expand Down
27 changes: 25 additions & 2 deletions cmd/aries-js-worker/src/worker-impl-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ self.importScripts("./agent-rest-client.js")
postMessage({topic: "asset-ready"})

// TODO synchronized access to controller
let handler, controller
let handler, controller, notifier

const wsNormalClosureCode = 1000

const ariesHandle = {
aries: {
Expand All @@ -22,6 +24,13 @@ const ariesHandle = {
return newResponse(data.id, null, "'agent-rest-url' is required");
}

if (data.payload["agent-rest-wshook"]){
notifier = new wsnotifier(data.payload["agent-rest-wshook"], (msg) => {
postMessage(msg)
});
}


controller = new RESTAgent.Client(data.payload["agent-rest-url"], data.payload["agent-rest-token"]);
return newResponse(data.id, "aries is started");
},
Expand Down Expand Up @@ -67,4 +76,18 @@ function newResponse(id, payload, errMsg, topic) {
errMsg: errMsg,
topic: topic
};
}
}

const wsnotifier = class {
constructor(url, postMsg) {
this.socket = new WebSocket(url);
this.socket.addEventListener('message', function (event) {
// TODO REST agents are not currently revealing topic information on incoming messages,
// Once REST supports this feature, topic value will be dynamic. [Issue #1323]
postMsg(newResponse(Math.random().toString(36).slice(2), event.data, "", "all"));
});
}
stop(){
this.socket.close(wsNormalClosureCode, "stopped notifier in aries-js-worker")
}
};

0 comments on commit e4d0b76

Please sign in to comment.