Solace Messaging API for JavaScript

10.18.3 · active · verified Wed Apr 22

The `solclientjs` package provides a JavaScript API for connecting Node.js, browser, and mobile applications to a Solace Event Broker. It enables applications to send and receive messages using various messaging patterns like publish/subscribe. The current stable version is 10.18.3, and it follows a regular maintenance and update cadence, often tied to Solace Event Broker releases. Key differentiators include its focus on high-performance messaging specifically with Solace Event Brokers, offering distinct API variations (Debug, Full, Production) that balance performance optimization, minification, and logging verbosity. This library is designed for robust enterprise messaging environments, providing a programmatic interface to Solace's advanced messaging capabilities. It is proprietary software intended solely for use with a Solace Event Broker under specific license terms.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to connect to a Solace Event Broker, subscribe to a topic, and publish a message using the `solclientjs` API.

import * as solace from 'solclientjs';

const host = process.env.SOLACE_HOST ?? 'localhost:55555';
const vpn = process.env.SOLACE_VPN ?? 'default';
const user = process.env.SOLACE_USERNAME ?? 'client_username';
const password = process.env.SOLACE_PASSWORD ?? 'client_password';

const sessionProperties = new solace.SessionProperties();
sessionProperties.url = host;
sessionProperties.vpnName = vpn;
sessionProperties.userName = user;
sessionProperties.password = password;

const session = solace.SolclientFactory.createSession(sessionProperties,
    new solace.MessageRxCBInfo((session, message) => {
        console.log(`Received message: ${message.getSdtContainer().getValue()} from topic: ${message.getDestination().getName()}`);
        message.destroy(); // Important to destroy messages after processing
    }),
    new solace.SessionEventCBInfo((session, event) => {
        console.log(`Session Event: ${event.sessionEventCode} - ${event.infoStr}`);
        if (event.sessionEventCode === solace.SessionEventCode.UP_NOTICE) {
            console.log('Session connected successfully.');
            session.subscribe(
                solace.SolclientFactory.createTopic('tutorial/topic'),
                true, // generate confirmation
                'my-subscription', // correlation key
                10000 // timeout (ms)
            );
            const message = solace.SolclientFactory.createMessage();
            message.setDestination(solace.SolclientFactory.createTopic('tutorial/topic'));
            message.setSdtContainer(solace.SDTContainer.create('Hello from solclientjs!'));
            session.send(message);
            console.log('Message sent to tutorial/topic');
        } else if (event.sessionEventCode === solace.SessionEventCode.DISCONNECTED) {
            console.log('Session disconnected.');
            session.destroy();
        }
    })
);

try {
    session.connect();
    console.log('Attempting to connect to Solace broker...');
} catch (error: any) {
    console.error(`Error connecting: ${error.message}`);
}

// Keep the process alive for a bit to receive messages
setTimeout(() => {
    if (session.isConnected) {
        session.disconnect();
    }
    console.log('Exiting example.');
    process.exit(0);
}, 10000); // Disconnect after 10 seconds

view raw JSON →