SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How live chat works ?

    Hi,

    I am very keen about live chats but i am not sure how they work ? How the messages etc. are updated without refreshing windows etc. So after a bit of research i came to the conclusion that ajax is being used and it works in the following way:

    1. We put javascript code in our html pages so when a user comes on the webiste the javascript updates the database with user's details.

    2. On admin/operator side the data is then retrieved from database and displayed.

    3. If user/admin initiates the chat then both get a window. In which if user posts a message it is submitted to database and on the admin side using ajax etc. it keeps on checking for new messages. If there's a new message it picks it up and add's to the text box.

    4. Same happens on user side..if admin posts a message it gets added to db and user's window keeps on checking for new messages from db using ajax and if new is available then its displayed in the textbox.

    Please correct me if i am wrong.

    Thanks.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tapan View Post
    1. We put javascript code in our html pages so when a user comes on the webiste the javascript updates the database with user's details.
    Not exactly. When a user comes to your website, the serverside (PHP) updates the database. You might use javascript for the login process and then send the user data to the server using AJAX, but still the updating is done by the server side scripting language, not JS.

    2. On admin/operator side the data is then retrieved from database and displayed.
    Yes. But the admin side will have to periodically check the database using AJAX.

    3. If user/admin initiates the chat then both get a window.
    No. The user side has to check periodically to see if the admin has started a chat session and then show the window.

    In which if user posts a message it is submitted to database and on the admin side using ajax etc. it keeps on checking for new messages. If there's a new message it picks it up and add's to the text box.
    Yes. User posts are submitted to the server using AJAX.

    4. Same happens on user side..if admin posts a message it gets added to db and user's window keeps on checking for new messages from db using ajax and if new is available then its displayed in the textbox.
    Yes.

  3. #3
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    1. Not exactly. When a user comes to your website, the serverside (PHP) updates the database. You might use javascript for the login process and then send the user data to the server using AJAX, but still the updating is done by the server side scripting language, not JS.

    How is it possible that php updates the database ? For eg. if you see currently available live chat softwares they ask you to put js code in your html pages. What if the page is .htm and not .php ? Then how will it be updated server side ?

    Thanks.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    It is important to keep in mind what is client side and what is server side.

    Client side is the browser, that receives a HTML/CSS/JS page and displays it to the user. It sends requests to the server side (using AJAX, or requesting a whole new page, or whatever).

    Server side is the scripting language (PHP) and database (MySQL). The script (PHP) queries the database and creates a HTML/CSS/JS page which is sent to the client.

    Now, if the JS for example uses AJAX to send messages or check and eventually display new messages from other users, what happens is that a request is sent to the server. That request contains the name of the server side script (PHP) to be executed and the name of the client side function (JS) that will receive the result of the request.

    The server side script that has been called by the AJAX request is executed, queries the DB, inserts data in the DB, whatever, and creates an output (XML, HTML, whatever) and sents it back to the client.
    On the client side, the result of the request is given to the JS function which then displays it to the user, or whatever.

    I hope this is clear

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Has guido has illuded Javascript cannot access your server side database directly. For example the following JS code is impossible:
    Code JavaScript:
    var query = "INSERT INTO `table` (col1) VALUES ('value1')";
    if(mysql_query(query)) alert("Insert Success!");

    JavaScript is executed in the context of the client side web browser. The (MySQL) DB is millions of kilometers away on a server without a monitor.
    JS cannot send commands directly to MySQL. What it can do is initiate an XHR (Ajax) request over HTTP which calls a script on the server (and passes data via POST) which can then access the database.

    Generally the server has no mechanism to "send" data to the client via an Ajax like request. The client must periodically check (poll) the server to see if there's any new content and pull it in that way.
    I think there are systems that can "push", but I haven't looked into these and don't really understand them. Search http://ajaxian.com if you're curious.


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
  •