-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding example on how to integrate a Waku node in NodeJs
- Loading branch information
1 parent
069c1ad
commit 004e4bd
Showing
8 changed files
with
675 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,4 @@ nimbus-build-system.paths | |
*.sqlite3-shm | ||
*.sqlite3-wal | ||
|
||
/examples/nodejs/build/ |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "waku", | ||
"sources": [ "waku_addon.c", "../cbindings/base64.c" ], | ||
"libraries": [ "-lwaku", "-L../../../build/" ] | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
var express = require('express'); | ||
var app = express(); | ||
|
||
function create_random_string(length) { | ||
let result = ''; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
const charactersLength = characters.length; | ||
let counter = 0; | ||
while (counter < length) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
counter += 1; | ||
} | ||
return result; | ||
} | ||
|
||
var wakuMod = require('bindings')('waku'); | ||
|
||
var cfg = `{ | ||
"host": "0.0.0.0", | ||
"port": 60001, | ||
"key": "364d111d729a6eb6d3e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9", | ||
"relay": true | ||
}` | ||
|
||
function event_handler(event) { | ||
console.log("evento NodeJs: " + event) | ||
} | ||
|
||
wakuMod.wakuVersion(function(msg){ console.log("Waku Version: " + msg) }) | ||
|
||
// Example on how to retrieve a value from the waku library | ||
var defaultPubsubTopic = "" | ||
wakuMod.wakuDefaultPubsubTopic(function(msg){ defaultPubsubTopic = msg }) | ||
|
||
console.log("Setting callback event callback function") | ||
wakuMod.wakuSetEventCallback(event_handler) | ||
wakuMod.wakuNew(cfg) | ||
|
||
wakuMod.wakuStart() | ||
|
||
wakuMod.wakuConnect("/ip4/127.0.0.1/tcp/60000/p2p/16Uiu2HAmVFXtAfSj4EiR7mL2KvL4EE2wztuQgUSBoj2Jx2KeXFLN", | ||
10000, | ||
function onErr(msg) { | ||
console.log("Error connecting node: " + msg) | ||
}) | ||
|
||
wakuMod.wakuRelaySubscribe(defaultPubsubTopic, | ||
function onErr(msg) { | ||
console.log("Error subscribing: " + msg) | ||
}) | ||
|
||
app.post('/publish', | ||
function (req, res) { | ||
// First read existing users. | ||
console.log("Publish event received") | ||
|
||
wakuMod.wakuRelayPublish(defaultPubsubTopic, | ||
"content_topic_name", | ||
create_random_string(10), | ||
10000, | ||
function onError(msg) { | ||
console.log("Error: " + msg) | ||
process.exit(-1) | ||
}); | ||
|
||
res.end( JSON.stringify("OK publish")) | ||
}) | ||
|
||
var server = app.listen(8081, function () { | ||
var host = server.address().address | ||
var port = server.address().port | ||
console.log("Example waku listening at http://%s:%s", host, port) | ||
}) |
Oops, something went wrong.