Solace Messaging API for JavaScript
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
-
Error connecting: Solace error 500: Connection refused (client-connect-failed)
cause Incorrect broker host/port, invalid VPN, username, or password, or broker is unreachable/down.fixVerify `sessionProperties.url`, `vpnName`, `userName`, `password` against your Solace Event Broker configuration. Ensure the broker is running and accessible from the client's network. -
Session Event: 18 - Subscription Error: Client does not have 'subscribe' permission for topic 'some/forbidden/topic'.
cause The client attempted to subscribe to a topic for which it lacks permissions on the Solace Event Broker.fixCheck topic permissions for the client username and ensure the topic syntax is correct and allowed by the broker's Access Control Lists (ACLs). -
TypeError: solace.SolclientFactory.createSession is not a function
cause Incorrect import or access method for the `SolclientFactory`. The `solace` object might not be correctly resolving the `SolclientFactory` property.fixEnsure you are importing `solace` correctly as `import * as solace from 'solclientjs';` for ESM/TypeScript or `const solace = require('solclientjs');` for CommonJS. `SolclientFactory` should then be accessible as a property of the main `solace` object.
Warnings
- gotcha Proprietary Software License
- gotcha External Documentation
- gotcha API Variation Selection
- gotcha Mandatory Message Destruction
Install
-
npm install solclientjs -
yarn add solclientjs -
pnpm add solclientjs
Imports
- solace
import solace from 'solclientjs';
import * as solace from 'solclientjs';
- solaceDebugAPI
import { debug } from 'solclientjs';import * as solace from 'solclientjs'; const solaceDebugAPI = solace.debug;
- Session, Message, SessionEventCode
import { SolaceSession, SolaceMessage } from 'solclientjs';import { Session, Message, SessionEventCode } from 'solclientjs';
Quickstart
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