🎄 Happy Holidays! 🥳
Most of Solace is closed December 24–January 1 so our employees can spend time with their families. We will re-open Thursday, January 2, 2024. Please expect slower response times during this period and open a support ticket for anything needing immediate assistance.
Happy Holidays!
Please note: most of Solace is closed December 25–January 2, and will re-open Tuesday, January 3, 2023.
Can we use Solace connection pool for node.js application using solaceclient library
Hi Team,
we are building an Azure Function using node.js. This azure function is connecting to Solace using SolaceClient library and publishing a message into Solace topic. Currently, we are open and close the connection for a request. We want use the Connection Pooling concept in Solace Connectivity. so that, same connection can be used in different request.
Is there any way in can use the connection pooling? if yes, please provide us some documentation/github sample codebase.
if no, what is the best way to utilize the connectivity and get better performance or is it sufficient what ever we have implemented?
Thank you in advance!
Thanks,
Sibendu
Answers
-
Greetings @Sibendu,
I am not sure exactly what type of Azure Function template you are using, but in general, objects defined outside of the function scope callback will persist between invocations of the Azure function calls. As long as the session instance is hoisted to the top level scope, it can be re-used multiple times.
Note, that there are no guarantees on lifecycle - keeping the session open forever will keep the function instance alive for some maximum timeout, but it is probably better to explicitly close the connection after a defined interval and reconnect if that specific container happens to be reused.
Here is a sample of an HTTP triggered Azure function that publishes a message to Solace, and will re-use the same connection if it is called again within 8 seconds:
const { app } = require('@azure/functions'); const solace = require('solclientjs'); const brokerOptions = { url: "wss://HOSTNAME.messaging.solace.cloud:443", vpnName: "default", userName: "solace-cloud-client", password: ". . ." }; function createSolacePublisher() { const factoryProps = new solace.SolclientFactoryProperties(); factoryProps.profile = solace.SolclientFactoryProfiles.version10; solace.SolclientFactory.init(factoryProps); let publishCount = 0; let session = null; function initSession() { return session ? Promise.resolve() : new Promise((resolve) => { session = solace.SolclientFactory.createSession(brokerOptions); session.on(solace.SessionEventCode.UP_NOTICE, resolve); session.connect(); }); } let interval; function scheduleCloseSession() { if(interval) { clearInterval(interval); } interval = setInterval(() => { session.disconnect(); publishCount = 0; session = null; }, 8000); } return { publishToTopic: async function(topic, payloadText) { await initSession(); const message = solace.SolclientFactory.createMessage(); const publishDestination = solace.SolclientFactory.createTopicDestination(topic); message.setDestination(publishDestination); message.setBinaryAttachment(payloadText); session.send(message); publishCount++; scheduleCloseSession(); }, getPublishCount: function() { return publishCount; } }; } const publisher = createSolacePublisher(); app.http('httpTriggerDemo', { methods: ['GET', 'POST'], authLevel: 'anonymous', handler: async (request, context) => { context.log(`Http function processed request for url "${request.url}"`); await publisher.publishToTopic('try-me', 'Azure Function Message'); return { body: `Published Message to Solace! Publish Count: ${publisher.getPublishCount()}` }; } });
1