How to get the connection's id in the client?

Is there a proper way of getting the connection id in the cliente?

I’m aware of Meteor.connection._lastSessionId, but maybe there is a better way?

Usually the connection is the correct object for these kind of things so I think you are in the right direction.

Curious to know why do you need the session id. What is the use case?

To store temporary data from the session.

1 Like

Found a way of doing that, here’s my React code:

import { useTracker } from "meteor/react-meteor-data";

import { useState } from "react";

const useMeteorId = () => {
  const [id, setId] = useState<string | null>(null);

  useTracker(() => {
    const connected = Meteor.status().connected;

    if (!connected) {
      setId(null);
    } else {
      setTimeout(() => {
        const sessionId = Meteor.connection?._lastSessionId;
        setId(sessionId || null);
      }, 1);
    }
  }, []);

  return id;
};

export default useMeteorId;

There’s room for improvement, but for my use case that’s fine :slightly_smiling_face: