npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nuskin/ns-solace-utils

v4.1.0

Published

This is a library to allow you to produce and consume in the Solace environment.

Downloads

642

Readme

ns-solace-utils

Utility library for Nu Skin to publish to and consume from the Solace Event Message Router.

CONTENTS

NPM and Github Repo

https://www.npmjs.com/package/@nuskin/ns-solace-utils https://code.tls.nuskin.io/nextgen-development/utility/ns-solace-utils

Installation

Using npm:

$ npm install @nuskin/ns-solace-utils

Usage

Solace Sessions

The session module creates and returns a solace session based on the properties you provide. It can also disconnect and exit from a session.

When you get a session you are required to pass in a callback that it will trigger when the session receives the SessionEventCode.UP_NOTICE event from Solace. You also have the option to override the other events that it is currently listening for if you would like. Subscribed Solace Events: SessionEventCode.ACKNOWLEDGED_MESSAGE, SessionEventCode.REJECTED_MESSAGE_ERROR, SessionEventCode.CONNECT_FAILED_ERROR, SessionEventCode.DISCONNECTED You are also given the session object itself if you want to just override it inside your own code by calling session.on()

To disconnect or exit your session you can just call those methods, pass it your session, and it will gracefully disconnect and dispose of your session for you. Example 1:

const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const properties = {};

getSolaceSession(properties, (session) => {
    // What you want to do once the session is up and connected goes here.
}).connect();

    

Example 2:

const { getSolaceSession, exitSolaceSession, disconnectSolaceSession } = require('@nuskin/ns-solace-utils').session;
const properties = {};

const solaceSession = getSolaceSession(properties, onUpCallback);
solaceSession.connect();

disconnectSolaceSession(solaceSession);
exitSolaceSession(solaceSession);

const onUpCallback = (session) => {
    // You could also split them up this way if you'd prefer
}
    

Example 3:

const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const properties = {};

const solaceSession = getSolaceSession(properties, onUpCallback, onAcknowledgedCallback);
solaceSession.connect();

const onUpCallback = (session) => {
    // You could also split them up this way if you'd prefer
}

const onAcknowledgedCallback = (session) => {
    // I'm overriding SessionEventCode.ACKNOWLEDGED_MESSAGE here.
}

Queue Message Consumer

The queueConsumer module the ability to begin consuming messages from a Solace Queue. The Solace MessageConsumer does require an active session so you'll need to pass that in once you have it. Like with the session module you will be required to pass in a callback. In this case the callback will listen for the MessageConsumerEventName.MESSAGE event. This is what is fired when your consumer receives a message from the queue. Also, just like the session module we allow you to override the other default callbacks. Subscribed Solace Events: MessageConsumerEventName.UP, MessageConsumerEventName.CONNECT_FAILED_ERROR, MessageConsumerEventName.DOWN, MessageConsumerEventName.DOWN_ERROR

The Solace Message Router doesn't allow us to easily nack messages so the default behavior of these other events is to disconnect from the queue without acknowledging the message which will place the message back on the queue. Then we try and reconnect after a brief wait at which point your consumer will receive the same message again.

You can also pass in a MessageConsumerAcknowledgeMode. It will default to CLIENT which means that it is up to you to acknowledge() your own messages when you're done with them. The other option is MessageConsumerAcknowledgeMode.AUTO which will acknowledge your messages the moment you pull them from the queue.

Important Note: There are a variety of ways the message could come down from Solace depending on how the client sent the message body to the router. You can use the getMessageBody() function in the util.js file.

Example 1:

const { createQueueConsumer } = require("@nuskin/ns-solace-utils").queueConsumer;
const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const queueName = 'MyQueueName';

getSolaceSession(properties, (session) => {
    const queueConsumer = createQueueConsumer(session, queueName, (message) => {
        // This is the callback that will be triggered when you receive a message
    })
    queueConsumer.connect();
}).connect();

Example 2:

const { createQueueConsumer } = require("@nuskin/ns-solace-utils").queueConsumer;
const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const queueName = 'MyQueueName';

getSolaceSession(properties, (session) => {
    const queueConsumer = createQueueConsumer(session, queueName, onMessageEventCallback)
    queueConsumer.connect();
}).connect();

const onMessageEventCallback = (session) => {
    // You could also split them up this way if you'd prefer
}

Example 3:

const { createQueueConsumer } = require("@nuskin/ns-solace-utils").queueConsumer;
const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const queueName = 'MyQueueName';

getSolaceSession(properties, (session) => {
    const queueConsumer = createQueueConsumer(session, queueName, onMessageCallback, onUpCallback)
    queueConsumer.connect();
}).connect();

const onMessageCallback = (session) => {
    // You could also split them up this way if you'd prefer
}


const onUpCallback = (session) => {
    // I'm overriding the MessageConsumerEventName.UP event
}

Topic Message Publisher

The publisher module allows you to either send a message in a "fire and forget" fashion or it allows you to subscribe to the events and listen for the consumer of your message to send you a SessionEventCode.ACKNOWLEDGED_MESSAGE or a SessionEventCode.REJECTED_MESSAGE_ERROR. Once again you can override these functions with whatever you want and you'll also need to pass in your session again. Your messages need to be passed in as a string or you will experience an error or potential issues with your consumers.

The default event listeners will log out whether or not your message was successful and then gracefully disconnect and dispose of your session. So, if you want to do anything based on the consumer's event you'll want to pass in some callbacks.

Example 1:

const { sendMessage } = require('@nuskin/ns-solace-utils').publisher;
const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const topicName = 'MyTopicName';
const message = { readmeMessage: `I'm JSON right now but make sure you stringify me!`}
const metadata = {}

getSolaceSession(properties, (session) =>
    sendMessage(session, topicName, JSON.stringify(message), metadata);
).connect();

Example 2:

const { sendMessageAndSubscribe } = require('@nuskin/ns-solace-utils').publisher;
const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const topicName = 'MyTopicName';
const message = { readmeMessage: `I'm JSON right now but make sure you stringify me!`}
const metadata = {}

getSolaceSession(properties, (session) =>
    sendMessageAndSubscribe(session, topicName, JSON.stringify(message), metadata, (evt) => {
        return 'acknowledged';
    }, (err) => {
        return 'rejected';
    });
).connect();

Example 3:

const { sendMessageAndSubscribe } = require('@nuskin/ns-solace-utils').publisher;
const { getSolaceSession } = require('@nuskin/ns-solace-utils').session;
const topicName = 'MyTopicName';
const message = { readmeMessage: `I'm JSON right now but make sure you stringify me!`}
const metadata = {}

getSolaceSession(properties, (session) =>
    sendMessageAndSubscribe(session, topicName, JSON.stringify(message), metadata, onAcknowledgedCallback, onRejectedCallback);
).connect();

const onAcknowledgedCallback = (session) => {
    // I'm overriding the SessionEventCode.ACKNOWLEDGED_MESSAGE event
}

const onRejectedCallback = (session) => {
    // I'm overriding the SessionEventCode.REJECTED_MESSAGE_ERROR event
}