Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Simple Chat Application

    Hi, so for purely educational purposes I have been wanting to build a simple chat application. My first intentions where to create a simple form based application and sent and received messages to my database, but this leads me to some questions.

    Even if I use AJAX to pull any new messages from the database, how would I be able to make the pulls every few seconds while not overloading MySQL with to many connections?

    Not only how would I do this with out overloading MySQL, but killing my hosting bandwidth?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Well, you WILL kill your bandwidth if you have 7,348 simultaneous users. But a handful of users? Each hitting the server with an "anything new" request every (say) 3 to 5 seconds? Noise.

    Do I assume you will be coding this in PHP? So does PHP have the equivalent of ASP and ASP.NET's "Application" object? Basically, in those technologies, there is an object that *ALL* threads/pages have acccess to on a shared basis. There's a simple locking mechanism so that one user can change the contents while forcing all others to wait, thus avoiding deadlocks. If you have that capability--or can build it--then the easy answer is that when a user *posts* a message, you add it to the DB, yes, but you *ALSO* add it to an array that is stored at that global level (using lockout, of course). And you timestamp it.

    So now when a user's AJAX (or equivalent...consider using an <IFRAME> or maybe even hidden <IFRAME>) hits the site every 3 seconds you only have to ask the application-scope object "what's the latest timestamp". The caller supplies the timestamp of the last message that the caller has knowledge of, of course, and now a simple comparison allows you to send back any new messages.

    VERY low DB overhead.

    You can, of course, do the equivalent all in the DB. And chances are that if you have any decent caching at all for that DB then the table you are hitting to look for updates will still be "hot" in memory, so it's not really going to be a too-heavy DB hit. But if you can avoid the DB hit, do so.

    If you are going to support multiple simultaneous but separate chats, you COULD still use the global app object, but it might be much easier to just use the DB. Depends on what capabilities the hypothetical PHP global scope object/collection/whatever has.

    I could write this in ASP in about 30 minutes, and I'd just use an <IFRAME> for the chat area and have the server simply refresh the <IFRAME> each time. Wouldn't be efficient, but would sure be up and running quickly. (Oh, yes, and would support simultaneous separate conversations.)

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Yeah PHP and MySQL is what I use with my hosting. I guess I might have been overly worried cause I have never ran so many requests to and from my database before so I just didn't know what my results in both bandwidth and database performance would be like.

    Thanks for the advise Old Pedant good looking out.

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Standard default for simultaneous mysql connections is 150 IIRC. Don't know what server load that creates, but I'm guessing if that would be large overhead they'd use a lower number.

    mysql was set up originally to be consumer/user friendly and fast.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Our installation only had 100, using MySQL with Tomcat and Apache.

    But that's PLENTY! Tomcat really bogs down if you give it much more than 20 to 50 threads (depending on the processor[s]). So even with 50 threads [simultaneous pages being served], that allows for 2 connections per thread. In a chat app, I can't see why you'd need more than 1 per thread. We ran stress tests on that configuration with 10,000 simulated clients and never had a real hiccup (or at least none that weren't the fault of our code).

    I'd be real surprised if PHP and Apache didn't perform quite similarly.

  • #6
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    I also simulated thousand records on my end. With 10 connections going at once. I was receiving a my_sql error about to many connections but after I added the close connection potions to my get and post everything seems to be running smooth.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    LOL! Yeah, unlike ASP, if you don't close connections in JSP or PHP you are hosed. Sloppy coding of the frameworks, in my opinion. It wouldn't be hard for the frameworks to keep track of which threads opened a connection and auto-close them when the thread went away or change what page it is servicing. Now, granted, doing it the ASP way leads to sloppy user programming, but I think that's better than taking a site down because of a rare code path that for some reason doesn't get the connection closed.


  •  

    Posting Permissions

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