Check the condition before connecting the Socket

Hello HeroicLabs Team,
In Nakma Server,

  1. How do I set up a condition in the server to check the condition for allowing socket connections. Check the condition like Before for API calls.
  2. Is there any way to check the list, disconnect which users are connecting Socket to Nakama Server?

Thanks.

  1. Sockets are always available. What exactly are you trying to achieve by having a condition? Is this because you’ve forked Nakama to make sockets conditional somehow?
  2. You want to disable socket connectivity entirely? Why is that? The best way to do this is to disallow connections to the /ws path (at the loadbalancer level)

Hi, Thanks for your reply.

  1. I want to limit the number of socket connections to nakama server: 1000 CCU. So, I will need to write a func that checks the number of connections to peak before allowing the connection. If CCC > 1000, it will reject the connection.
  • Is there any func that reads the current number of connections (As I am using nakama, only when the user authenticates and makes a socket connection, will the admin display Sessions Presences, right? If so, how can I read this data)

  • Is there any function to set a condition before allowing the user to connect to the socket?

  1. I want to write a func that can kick any user from the socket connection. I have seen: nk.SessionDisconnect(), this func can disconnect the user and disconnect their socket, right?.

Thank you.

Hi,
Can you answer me?

We’ve never had any requests to artificially limit the number of connections to Nakama. Why would you want to do that?

I am also curious about the core question: can we run custom logic before accepting a socket connection?

For example, in a typical socket.io / express project, you do something like this:

// Middleware to validate connection
const validateConnection = (socket, next) => {
  // Extract authentication token from handshake
  const token = socket.handshake.auth.token;

  try {
    // Example authentication logic
    if (!token) {
      return next(new Error('Authentication error: No token provided'));
    }

    // Validate token (replace with your actual token verification logic)
    const isValidToken = verifyToken(token);

    if (!isValidToken) {
      return next(new Error('Authentication error: Invalid token'));
    }

    // Optional: Attach user information to the socket
    socket.user = decodeToken(token);

    // Allow the connection
    next();
  } catch (error) {
    // Handle any unexpected errors during authentication
    next(new Error('Authentication error: ' + error.message));
  }
};

// Apply middleware globally to all socket connections
io.use(validateConnection);

That auth is just an example, in reality it’s any code we want to determine if we want the user to connect to the socket.

Based on the documentation I checked, I could only find that we can do this after the socket connection is established, but not before the socket connection is accepted.

Hi,
I want to check before allowing socket connection. Reason: I need to limit the number of players at a time (CCU) to about 1000, If exceeded, only allowed players can open socket connection.

I will post this useful documentation page:

I believe you want to use the hooks with Rt in them (standing for real-time I assume).

The one that looks like the earliest point is:

initializer.registerBeforeRt(guardFunction);

I’m not working with sockets at the moment, so you will have to test at which point this is being ran.