0

Current Design

I'm making a chatting app where any 2 random users can talk.

On the server (in php), I have an array where I store the client pairs, and the key used to access these pairs is one of the client's IDs.

The reason I don't like this is I have to store the client pair in the array twice. I don't know which client is going to disconnect first, so I hash it twice, once for each client id.

In case this isn't clear yet, the process is: client A and B are chatting. Client B disconnects, so I access the pair with key B, figure out the other client's id is A, then unset both elements using keys A and B.

Question

Any better ideas ? It would be nice if 2 keys could be used to access the exact same element in an array, but I don't think that exists.

p.s.

(This client pair object may sound useless based on the description, but it also holds each client's corresponding socket which I can use to send messages to from the server when a disconnection occurs.)

How I'm picturing this with code:

/* The server receives a message from a client with id 1000 that he has left chat */

$client_pairs = array();  //map holding all client pairs currently chatting

connectClients( $client1, $client2 )
{
    $client_pairs[$client1 -> id] = array( $client1, $client2 );
    $client_pairs[$client2 -> id] = array( $client2, $client1 );
}

disconnectClient( $client_id )
{
    $client_pair = $this -> client_pairs[$client_id] 
    $client2 = $client_pair[1];

    unset( client_pairs[$client_id] );
    unset( client_pairs[$client2 -> id] );

    /*
       do stuff with the $client_pair
    */
}
2
  • You're question is rather broad, could you supply some code (pseudo) to describe how you achieve/don't achieve what you're trying to do? Commented Feb 17, 2015 at 1:24
  • @Darren Sure. give me 2 mins. Commented Feb 17, 2015 at 1:28

1 Answer 1

1

You could have two arrays: $users and $sessions. Let's say, for example, that you, have two users: client_id1 and client_id2. You could then:

$sessions[] = your session information, e.g. sockets, plus both client_ids
end($sessions);
$sessId = key($sessions);
$users[$cliend_id1] = $sessId;
$users[$cliend_id2] = $sessId;

When someone disconnects, use their ID as:

$sess = $sessions[$users[$id]];

$sess then should have everything you need.

Sign up to request clarification or add additional context in comments.

5 Comments

I gave you an upvote for now. The only problem with this is this assumes a user is only involved in 1 session at a time. The worst case scenario is that a user could be logged in with 2 different devices, and thus, be in 2 sessions. But this did reinforce an idea I had earlier of just having the client send the session id to the server for whichever session he's disconnecting from. That way I could support any number of sessions.
Use $users[$licent_id] = array(one or more session IDs).
But how would you know which session the user is disconnecting from? It could any one of them within the array.
I understand, you would need to get that at disconnect, but I was just suggesting how to track multiple session IDs for a single user if that was useful. As long as the session has both client_ids, you can always find out what you need.
@bodbye Okay I'll just accept this since it was the (only) most useful answer. I'm going to generate a session id on the server side whenever 2 clients connect, send that to both clients (along w other info), then when a client disconnects, they'll send the server the session id they're disconnecting from. And for ungraceful disconnections( i.e. a phone shutting off ), I'll just remove them from all sessions they're involved in (using your suggestion). Thanks for bouncing ideas off with me (which kind of what I needed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.