Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0

    Post Best way to implement AJAX chat in PHP?


    Hello,

    May I know please what is the best way to implement the AJAX chat? I used to make the normal way where I send AJAX requests every 1 second to see if new messages arrive to grab them to HTML.

    But I feel it is not a practical way specially when the database gets huge.

    I never worked on the socket chat but I think the AJAX is better because of the compatibility and the control.

    Are there any other ways better for performance? I have heard about the long polling and the comet but couldn't find a good details and examples about them.

    Would appreciate your precious hints.

    Thanks and regards
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,110
    Rep Power
    119
    Originally Posted by Moderns
    But I feel it is not a practical way specially when the database gets huge.
    Why not look into MySQL Temporary Tables. That way; you are not inserting massive amount of data in a permanent database's data table; with your AJAX chat.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0
    Originally Posted by web_loone08
    Why not look into MySQL Temporary Tables. That way; you are not inserting massive amount of data in a permanent database's data table; with your AJAX chat.
    Thanks for your reply but I need to keep the chat stored in database foever like the Facebook messages. Would you please advise?

    Thanks.
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,110
    Rep Power
    119
    If the chat data is going to be stored in a permanent database; then I do not see what the advantage would be of one method over the other one; seems like the dilemma would still be the same, no matter which method you would be using; at a certain point; you would have to look into database maintenance; like compressing the data or restructuring your database/data table(s) or purging unnecessary data or using multiple databases or etc; once your database reaches it maximum capacity.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0
    Thanks again. But in the normal case I have to send a request to DB and I have to receive a result (2 way communication). Is there any way to make HTTP push direct from the server without the need of sending AJAX request each time to check chat updates (1 way communication)?
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    It's possible on modern browsers, but requires special support on the server side. You would not be able to do it on shared hosting, but if you have a dedicated server you could do it. The most reliably technology for bi-directional communication is HTML5 web sockets. Comet and long-polling never took off because they tend to have a lot of difficult-to-debug compatibility issues.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    first of all, I think your database discussion is a bit absurd. What you consider to be "big" most probably isn't even remotely big for any modern database system. How many messages do you expect? 100,000 per second?

    Also, those "temporary tables" only live during a single connection. Unless you're using special techniques to have the script or connection run permanently, this doesn't even make sense. The table would be dead before it could even hold another message.

    My question would be: Did you actually experience performance issues, or is this rather some kind of vague gut feeling that there might be performance issues some day (when you have your 100,000 messages per second)? The classical polling approach should work just fine for any small or medium-sized site. Apart from that: The "push" approach you're talking about is what WebSocket does.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I think he got temporary tables and memory tables mixed up, but as you say, the database size should be a non-issue.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0
    Originally Posted by E-Oreo
    It's possible on modern browsers, but requires special support on the server side. You would not be able to do it on shared hosting, but if you have a dedicated server you could do it. The most reliably technology for bi-directional communication is HTML5 web sockets. Comet and long-polling never took off because they tend to have a lot of difficult-to-debug compatibility issues.
    Thanks. Actually I need my application to be compatible with old browsers like IE6, IE7. So the web sockets can be run on old browsers like IE6 and IE7?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    first of all, I think your database discussion is a bit absurd. What you consider to be "big" most probably isn't even remotely big for any modern database system. How many messages do you expect? 100,000 per second?

    Also, those "temporary tables" only live during a single connection. Unless you're using special techniques to have the script or connection run permanently, this doesn't even make sense. The table would be dead before it could even hold another message.

    My question would be: Did you actually experience performance issues, or is this rather some kind of vague gut feeling that there might be performance issues some day (when you have your 100,000 messages per second)? The classical polling approach should work just fine for any small or medium-sized site. Apart from that: The "push" approach you're talking about is what WebSocket does.
    Thanks. Actually no one can predict how big the DB would be but I like to work on a healthy/correct approach from the beginning to avoid any modifications later! So my concern now to know if the web sockets can be run on old browsers like IE6?

    Also, if possible, could you please refer me to any document where I can read about using the temporary tables in a chat process?

    Cheers,
  20. #11
  21. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,110
    Rep Power
    119
    Originally Posted by Moderns
    So my concern now to know if the web sockets can be run on old browsers like IE6?

    Also, if possible, could you please refer me to any document where I can read about using the temporary tables in a chat process?
    HTML5 WebSockets are only supported by IE10+; so you would not be able to use it in older versions of IE. And... you do not want to use temporary tables or memory tables; because you said you want to keep all these chat conversations in a permanent database. Storing these chats in a temporary/memory database / data table, would only store them for a limited amount of time (based which table storage method you implemented).
  22. #12
  23. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Thanks. Actually I need my application to be compatible with old browsers like IE6, IE7. So the web sockets can be run on old browsers like IE6 and IE7?
    No. WebSockets were added to IE in version 10.

    Also, if possible, could you please refer me to any document where I can read about using the temporary tables in a chat process?
    What you want is not temporary tables, it's memory tables. As Jacques1s said, temporary tables only persist for the duration of a single connection, which would be useless in your situation. Memory tables are tables stored in memory, which means both read and write access is extremely fast. However, it also means that the table data is deleted whenever MySQL stops running. Beyond that there is no difference between a normal table and a memory table.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Moderns
    Thanks. Actually no one can predict how big the DB would be but I like to work on a healthy/correct approach from the beginning to avoid any modifications later!
    Don't worry about modifications: If your chat actually becomes as popular as Facebook, changing the database backend will be your least problem.

    Until then, forget about those alleged performance issues. They only exist in your mind. Performance comes from a powerful server and proper database design (Are you using the right index at the right place?), it does not come from replacing polling with some other approach.

    Base your design decisions on hard facts, not gut feelings.

    Since you wanna support ancient browsers like IE6 (Who uses that??), you can forget about all those fancy techniques, anyway. You should be glad that you can at least use AJAX! It was pretty much invented at that time (well, a bit earlier).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0
    Originally Posted by Jacques1
    Don't worry about modifications: If your chat actually becomes as popular as Facebook, changing the database backend will be your least problem.

    Until then, forget about those alleged performance issues. They only exist in your mind. Performance comes from a powerful server and proper database design (Are you using the right index at the right place?), it does not come from replacing polling with some other approach.

    Base your design decisions on hard facts, not gut feelings.

    Since you wanna support ancient browsers like IE6 (Who uses that??), you can forget about all those fancy techniques, anyway. You should be glad that you can at least use AJAX! It was pretty much invented at that time (well, a bit earlier).
    Thank you very much. Very good points. When the work gets expanded, I will upgrade but now, I will stick to the AJAX short polling (two ways) with a robust database design.

    Cheers,
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    95
    Rep Power
    0
    Originally Posted by E-Oreo
    No. WebSockets were added to IE in version 10.


    What you want is not temporary tables, it's memory tables. As Jacques1s said, temporary tables only persist for the duration of a single connection, which would be useless in your situation. Memory tables are tables stored in memory, which means both read and write access is extremely fast. However, it also means that the table data is deleted whenever MySQL stops running. Beyond that there is no difference between a normal table and a memory table.
    Thank you very much for the brilliant clarifications. Greatly appreciated.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo