Skip to content

Commit

Permalink
add a test script for user to take reference from
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik-rishi committed Mar 25, 2024
1 parent d691d04 commit a01fcf0
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
98 changes: 98 additions & 0 deletions template/custom-handler-example/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { File } from '@asyncapi/generator-react-sdk';

export default function exampleCustomHandlerRegistrationScript() {
return <File name='script.js'>
{`
// output refers to the generated template folder
// You require the generated server. Running this code starts the server
// App exposes API to send messages
const { client } = require("../"); // library is in the current directory
client.init(); // starts the app
// Generated handlers that we use to react on consumer / produced messages are attached to the client
// through which we can register middleware functions
/**
*
*
* Example of how to process a message before it is sent to the broker
*
* Modify the following in the function \`testPublish\` according to your usecase
* 1. channel / topic name
* 2. tool used to publish message according to the procotol used (amqp/mqtt/etc ...)
* 3. name of the invocated middleware function (based on operation Id)
*/
function testPublish() {
// mosquitto_sub -h test.mosquitto.org -p 1883 -t "smartylighting/streetlights/1/0/action/12/turn/on"
// Registering your custom logic in a channel-specific handler
// the passed handler function is called once the app sends a message to the channel
// For example \`client.app.send\` sends a message to some channel using and before it is sent, you want to perform some other actions
// in such a case, you can register middlewares like below
client.registerTurnOnMiddleware((message) => { // \`turnOn\` is the respective operationId
console.log("hitting the middleware before publishing the message");
console.log(
\`sending turn on message to streetlight \${message.params.streetlightId}\`,
message.payload
);
});
client.app.send(
{ command: "off" },
{},
"smartylighting/streetlights/1/0/action/12/turn/on"
);
}
/**
*
*
* Example of how to work with generated code as a consumer
*
* Modify the following in the function \`testSubscribe\` according to your usecase
* 1. channel / topic name
* 2. tool used to publish message according to the procotol used (amqp/mqtt/etc ...)
* 3. name of the invocated middleware function (based on operation Id)
*
*/
function testSubscribe() {
// mosquitto_pub -h test.mosquitto.org -p 1883 -t "smartylighting/streetlights/1/0/event/101/lighting/measured" -m '{"lumens": 10}'
// Writing your custom logic that should be triggered when your app receives as message from a given channel
// Registering your custom logic in a channel-specific handler
// the passed handler functions are called once the app gets message sent to the channel
client.registerReceiveLightMeasurementMiddleware((message) => { // \`recieveLightMeasurement\` is the respective operationId
console.log("recieved in middleware 1", message.payload);
});
client.registerReceiveLightMeasurementMiddleware((message) => {
console.log("recieved in middleware 2", message.payload);
});
}
testPublish();
testSubscribe();
/**
*
*
* Example of how to produce a message using API of generated app independently from the handlers
* Again, will have to modify the below according to your usecase:
* 1. payload
* 2. channel / topic name
*
*/
(function myLoop (i) {
setTimeout(() => {
console.log('producing custom message');
client.app.send({percentage: 1}, {}, 'smartylighting/streetlights/1/0/action/1/turn/on');
if (--i) myLoop(i);
}, 1000);
}(3));
`}
</File>;
}
3 changes: 3 additions & 0 deletions template/package.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export default function packageFile({ asyncapi, params }) {

packageJSON = {
...packageJSON,
scripts: {
'test': 'node custom-handler-example/script.js'
},
main: './src/api',
dependencies,
};
Expand Down

0 comments on commit a01fcf0

Please sign in to comment.