dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: Design pattern of chat

  1. #1
    Join Date
    Jun 2008
    Posts
    136

    Design pattern of chat

    Hi,

    I've written a chat using ajax. In a nutshell, every few seconds, an ajax call is fired and data is getting updated. The ajax function is a setTimeout in itself, so the loop goes on.

    What's bothering me is that, when a page is open on my computer, it will send requests forever. So if I reduce setTimeout time to have more live/instant experience, I'd be firing a lot of redundant requests because most of them aren't necessary. I'd like to know if there's a way to only send the request if there's an update on the server. So instead of sending ajax and wait for response, just "listen" for a response?

    Maybe I'm trying to ask if there's a way to have a response on a page without "requesting by ajax"?
    All alternative ways are welcome.

    Thanks for your time.
    Last edited by asmith20002; 03-02-2013 at 03:24 AM.

  2. #2
    Join Date
    Feb 2013
    Posts
    5
    You can use a technique called "comet", which works with AJAX. In this technique the clients send requests to the server, and wait for an answer which will be send by the server only when there is some new information.

    some info about comet:
    http://en.wikipedia.org/wiki/comet_(programming)

    a demo code:
    http://stackoverflow.com/questions/3...g-example-code

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    EventSource and WebSockets are two newer tech that can provide a much better chat product than ajax.

    if you must use ajax, use long polling to keep the connection open until new data is ready to send.
    with an optimized long-poll. i've gotten the bandwidth waste down to ~200KB/day, including packet headers.

  4. #4
    Join Date
    Feb 2013
    Posts
    46
    Comet with PHP isn't really.. practical. I suggest using Node.JS with Socket-IO or something.
    http://socket.io/#browser-support

    You can run node.js and socket-io on a different port than Apache2.. such as 8080, and connect your web application to that port instead of the default port 80. That is, if you aren't using shared hosting. If your host only supports PHP, you are pretty much stuck doing it the inefficient way.. that is, continuous polling. You can work harder than you have to in order to implement some kind of long-polling program, but PHP and Apache2 are likely to break-down- despite the hours it will take to successfully implement it.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by s-p-n View Post
    Comet with PHP isn't really.. practical. I suggest using Node.JS with Socket-IO or something.
    http://socket.io/#browser-support
    i'm all about node, but why do you say it's impractical in PHP?

    it't the most common backend, whereas node has some deployment issue that are not trivial (ie root login, single-threaded)...
    Last edited by rnd me; 03-02-2013 at 08:13 PM.

  6. #6
    Join Date
    Jun 2008
    Posts
    136
    Thanks for the replies.

    I have root access and I use nginx and PHP. So basically the idea is making long-requests via AJAX, depending on my nginx timeout. But I'd need long-polling server so that it doesn't tie all my web server workers.

    I'll also checkout web sockets too. Thanks.
    p.s. How Google is doing its chat?

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by asmith20002 View Post
    Thanks for the replies.

    I have root access and I use nginx and PHP. So basically the idea is making long-requests via AJAX, depending on my nginx timeout. But I'd need long-polling server so that it doesn't tie all my web server workers.

    I'll also checkout web sockets too. Thanks.
    p.s. How Google is doing its chat?
    Server-Sent-Events (EventSource) is going to be WAY easier to impliment than sockets, and about as good for chat, since you can SEND from the client instantly.
    it's basically a simplified HTTP protocol.

    im sure google has their own custom java chat code like have for all their other stuff.
    we're not talking about your grandma's java, it's a custom build for custom hardware, very slick stuff.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles