SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Semaphores necessary for chat?

    I'm creating a chat as part of a site. The chat messages are stored in 50 entries in a MySQL database. All users will need to know which one of the 50 entries was entered most recently. So I thought that the variable which keeps that number was a critical resource. Which resulted in
    Code:
    Fatal error: Call to undefined function: sem_get() in ...Chatten.php on line 39
    All users in the chat simultaneously run a while-loop, and all need access to
    Code:
    $lastMsg = shm_get_var($shm,'lastMsg');
    Don't you have to use semaphores?

    Thanks in advance, Lars

  2. #2
    SitePoint Wizard wdmny's Avatar
    Join Date
    Jul 2000
    Location
    Here
    Posts
    1,010
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had to look up what a semaphore was, and I am still not clear on it, but I do not think you need to handle it that way. If you want to feed users the chat messages they don't have, track the last message they have and pull messages posted after that.

    Lets say your chat table has an ID, Text, and Date field. The user connects to chat and the script sends them the 50 most recent messages. Through Javascript, the last date/ID is stored. When calling for an update, the value of the last date/ID is passed to the script, and it fetches only new records. When the chat updates, the Javascript variable for the last date/ID is also updated.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't done anything in JavaScript yet. Does JavaScript allow for shared memory segments and semaphores? The standard argument for why semaphores are needed goes like this:
    1. The first user loads the $lastMessageIdNumber = e.g. 40
    2. A second user also loads the $lastMessageIdNumber = also 40
    3. The first user enters his message in the chat and tells the MySQL-database to update line number 41.
    4. The second user enters his message in the chat and tells the MySQL-database to update line number 41.

    Conclusion the message of the first user will vanish.
    (Note: When the users press the submit-button (in the chat-submit-message-page) all of the above occurs.)

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why on earth would you use a semaphore for something like that ?
    databases have locking-mechanisms build-in. in mysql for instance, you can use LOCK TABLE for the purpose.
    anyway - i think you problem lies on a more fundamental level. you can't run an infinite loop in php, that output's data to the client. php executes, output html, send html to client, client executes html. you can't loop this process. it's always server -> client -> stop. then the client might post back to the server. but that's a hole new request, and the php parser is closed down in between requests.
    Last edited by kyberfabrikken; Oct 11, 2004 at 09:19. Reason: typo

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In which scripting language do you program chat-pages then? Since the maximum executing time of a php page is 30 seconds. I know Java. Is it easy to interact with MySQL in Java?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •