Can we use Solace connection pool for node.js application using solaceclient library

Options
Sibendu
Sibendu Member Posts: 13 ✭✭

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

  • nicholasdgoodman
    nicholasdgoodman Member, Employee Posts: 33 Solace Employee
    edited March 15 #2
    Options

    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()}` };
      }
    });
    

  • Sibendu
    Sibendu Member Posts: 13 ✭✭
    Options

    Thank you for your response. Let us check from our end.

  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 494 admin
    Options

    Hey @Sibendu - did this response answer your original question?