0

I am making a PHP & MySQL (and CodeIgniter) video/chat program that 1) displays current online users, and 2) forces all users to disconnect when the moderator leaves.

However HTTP is stateless. What is the best method to implement the above features?

Should I use a heartbeat AJAX request for each user to determine the current state? How will I know if a user (such as the moderator) gets disconnected?

Exegesis
  • 1,028
  • 1
  • 18
  • 47

2 Answers2

1

This would require a close to real-time implementation. You could have a table that has all the rooms / chat sessions created. Then in your users table you could have a moderators column as well as a current room id column.

Rooms Table

Columns: ID, USER_ID, CREATED_AT, UPDATED_AT, NAME, PASSWORD, HAS_MODERATOR (bool) ....

When a moderator joins a room, you could update the "HAS_MODERATOR" field to true or 1. Then you could write a simple long-polling script that would simply make an ajax request to a php page or route and return a value of true or false. If it returns true, then a moderator is still in the room, otherwise you can close the room and force a redirect through javascript or something. The php script would simply check if has_moderator is true, AND you would go through the users table, retrieve all the moderators, and check what room they are in. Match the current room the moderator is in with the room your checking for. This will ensure that a moderator is in the room.

This might help you out: Notify Ajax/Javascript that background PHP has completed

Community
  • 1
  • 1
Daniel
  • 1,692
  • 2
  • 13
  • 19
1

I think you can implement it without using state logic of HTTP. You will need to store a current conversation state in database. Every user request will have a authenticated username cookie and a conversation id which will be used to pull the session from back end DB. Also, there will be a variable to check whether the moderator is present or not (heartbit style check). When a moderator leaves, simply change state of session which will for all others to logout. Let me know if you need any details of any step I suggested.

mrd081
  • 279
  • 2
  • 11