I'm building a web application using PHP, JavaScript/jQuery and MySQL. Right now I'm trying to implement a chat feature that allows two user who are both online to chat with each other (e.g. NOT a big chat room, but only private chats between two users). However, I've ran into the following questions during the implementation process:
How can I let one user know whether another user is currently online or not? Now I have a page where a user can see the name of other registered users. I hope to differentiate those who are currently logged in from those that aren't. Currently, when a user is logged in, I store his username in
$_SESSION['name']. So how can one user know whether another user's$_SESSION['name']is also set?How can I ensure that the conversation is private to two users? I currently have a page called
"chat.php", where the chat interface is located in. When one user clicks on the name of another user who's also online, the two will be directed to their own"chat.php". Similarly other users should be unable to view chat that they are not involved in. I'm currently thinking about generating a unique page for the two users, like"chat.php?user1=Tom&user2=John"But how exactly should I achieve this? I'm new to PHP.-
To display the new message if the other person has just sent one, can we do this using Ajax in an event-driven way? Or can we only use polling? I'm currently using polling like the following, but I feel that polling isn't every efficient:
// "logs.php" reads chat message from the database setInterval(function(){ $.get("logs.php", {}, function(resp) { // display the response }); }, 1000); // poll every second
Any input to any of the above questions is appreciated! Thanks.