How can I check session is connected or not (is session active/connected)? in solclientjs

prashantk2000
prashantk2000 Member Posts: 33 ✭✭
edited February 2023 in PubSub+ Event Broker #1

Hi All,

I do have few queries regarding the session lifespan in solclientjs lib.

  1. How can I check (periodically) session is active/connected or not?
  2. When connected using oauth2 and as soon as token expires then I see the session get disconnected, how does the client application get notified for disconnection instantly? I see it does reconnect for number of reconnect retries, is there any way that client can be notified instantly?

Thanks

Prashant

Tagged:

Answers

  • giri
    giri Member, Administrator, Employee Posts: 116 admin

    Hi @prashantk2000 - I am sure you might have already checked out the nodejs samples; if not, please refer to any of the nodejs samples in the tutorials sections.

    sample.session.on(solace.SessionEventCode.UP_NOTICE, function (sessionEvent) {
        sample.log('=== Successfully connected and ready to start the message consumer. ===');
        sample.startConsume();
    });
    sample.session.on(solace.SessionEventCode.CONNECT_FAILED_ERROR, function (sessionEvent) {
       sample.log('Connection failed to the message router: ' + sessionEvent.infoStr +
    	                ' - check correct parameter values and connectivity!');
    });
    sample.session.on(solace.SessionEventCode.DISCONNECTED, function (sessionEvent) {
       sample.log('Disconnected.');
       sample.consuming = false;
       if (sample.session !== null) {
          sample.session.dispose();
          sample.session = null;
       }
    });
    

    The Solace Node.js API communicates changes in status and results of connect and subscription calls through emitting session events with certain event names. The client can register callbacks to receive these SessionEvent and take necessary action. And you can control session connection-related parameters via SessionProperties your createSession call.

    I hope this helps - if your query is different, please elaborate.

  • prashantk2000
    prashantk2000 Member Posts: 33 ✭✭

    Thanks @giri ,

    I have few more queries as we are using OAuth2 Authentication for publishing/consuming,

    1. Session gets disconnected when the accessToken expires and it does the reconnect up to the reconnectRetries count but with same token which is already expired, is there way that we can pass the fresh JWT token for reconnect? I see it sends DOWN_ERROR after the reconnectRetries count exhaust, so do we require to call disconnect and dispose there?
    2. What would be the right approach to handle the session disconnection after token expires. Any suggestions?

    Thanks,

    Prashant

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 959 admin

    Hi @prashantk2000,

    Please check out Session.updateAuthenticationOnReconnect() which was added in v10.10.0

    Hope that helps!

  • prashantk2000
    prashantk2000 Member Posts: 33 ✭✭

    Thanks a lot @marc for sharing this, we able to resolve connectivity issue.