Listening to events in your JavaScript/NodeJs application: Promise/Async-Await vs Callbacks

Tamimi
Tamimi Member, Administrator, Employee Posts: 489 admin

There are a bunch of Solace specific status codes that could be leveraged in callback functions in your JavaScript application or NodeJS microservice. It's a common practice in JavaScript to handle async calls in a Promise/Async-Await implementation. The majority of the Solace API functions are asynchronous but are not implemented in a promise format since the solace broker emits a variety of events handled by the API as status codes to be leveraged in call-back functions.

So for example, to check if the connection is established with the solace broker, one will have to leverage the status code SessionEventCode#UP_NOTICE in a callback function since this event is only emitted on successful registration of a listener with the broker and could be used in the following format

solace.SessionEventCode.UP_NOTICE, function (sessionEvent) {
            *your logic here*
        }

More usecases could be found in the EventMonitor.js sample in our opensource NodeJS samples and the JavaScript samples github repos.

Let us now what events you are leveraging in your NodeJs and JavaScript applications!