Enhance waku_example.js

This commit is contained in:
Ivan Folgueira Bande 2024-01-13 20:11:29 +01:00
parent 76519c2651
commit 8ed0e0e0d5
No known key found for this signature in database
GPG Key ID: 3C117481F89E24A7
2 changed files with 63 additions and 38 deletions

View File

@ -1,5 +1,5 @@
{ {
"dependencies": { "dependencies": {
"nodejs-waku": "^0.1.11" "nodejs-waku": "^0.1.12"
} }
} }

View File

@ -1,51 +1,76 @@
// Please, export LD_LIBRARY_PATH="./node_modules/nodejs-waku/"
// before running the example so that the 'libwaku.so' can be properly linked.
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;
}
function event_handler(event) {
console.log("event rx: " + event)
}
var wakuMod = require('nodejs-waku'); var wakuMod = require('nodejs-waku');
wakuMod.wakuNew(`{
var cfg = `{ "host": "0.0.0.0",
"host": "0.0.0.0", "port": 60001,
"port": 60001, "key": "364d111d729a6eb6d2e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9",
"key": "364d111d729a6eb6d3e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9", "relay": true
"relay": true }`);
}`;
wakuMod.wakuNew(cfg);
wakuMod.wakuVersion(); wakuMod.wakuVersion();
/* // Example on how to retrieve a value from the waku library
var defaultPubsubTopic = ""
wakuMod.wakuDefaultPubsubTopic(function(msg){ defaultPubsubTopic = msg })
exports.wakuNew = function(cfg) { console.log("Default pubsub topic: " + defaultPubsubTopic)
wakuMod.wakuNew(cfg)
}
exports.wakuVersion = function() { console.log("Setting callback event callback function")
wakuMod.wakuVersion(function(msg){ console.log("Waku Version: " + msg) }) wakuMod.wakuSetEventCallback(event_handler)
}
exports.wakuShowDefaultPubsubTopic = function() { wakuMod.wakuStart()
// Example on how to retrieve a value from the waku library
var defaultPubsubTopic = ""
wakuMod.wakuDefaultPubsubTopic(function(msg){ defaultPubsubTopic = msg })
console.log("Default pubsub topic: " + defaultPubsubTopic)
}
exports.wakuSetEventCallback = function(eventHandler) { wakuMod.wakuConnect("/ip4/127.0.0.1/tcp/60000/p2p/16Uiu2HAmVFXtAfSj4EiR7mL2KvL4EE2wztuQgUSBoj2Jx2KeXFLN",
console.log("Setting callback event callback function") 10000,
wakuMod.wakuSetEventCallback(eventHandler) function onErr(msg) {
} console.log("Error connecting node: " + msg)
})
exports.wakuStart = function() { wakuMod.wakuRelaySubscribe(defaultPubsubTopic,
wakuMod.wakuStart() function onErr(msg) {
} console.log("Error subscribing: " + msg)
})
exports.wakuConnect = function(peerAddr, timeoutMs, onErr) { var express = require('express');
wakuMod.wakuConnect(peerAddr, timeoutMs, onErr) var app = express();
} app.post('/publish',
function (req, res) {
console.log("Publish event received")
exports.wakuRelaySubscribe = function(pubsubTopic, onErr) { wakuMod.wakuRelayPublish(defaultPubsubTopic,
wakuMod.wakuRelaySubscribe(pubsubTopic, onErr) "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 NodeJs waku listening at http://%s:%s", host, port)
})