Jump to content

AJAX and PHP chat application?


Adaaam

Recommended Posts

Hi all,

 

I need some guidance on how I can build an AJAX/PHP chat application, sort of like an instant messaging thing like the one on Facebook. It doesn't have to be anything fancy as long as it works. I have some ideas as to how I can build it but I've never tried to do anything like this before so I'm a bit lost. I'm confident I can build the entire PHP side of things up until I have to use AJAX, then I'm stuck.

 

The main concern I have is the affect it will have on the server, lets say I have a Javascript timer to check for new messages sent at certain intervals, wouldn't that have a massive affect on the server? I can only think of using a timer to get new messages for each user, I can't come up with any other way to get it to work. I just don't want to kill the server with AJAX requests if that's even possible.

 

So really my question is, would it be okay to use a timer to get new messages of each user at certain intervals, or is there another way of doing it?

 

Hope this makes sense :)

 

Thanks,

Adam

Link to comment
Share on other sites

Without knowing anything about the server or it's connection to the clients, or the number of clients you ware targeting it's impossible to resonable deductions to the effect that your application would have on said server.  So moving on to the rest of the question, you want to look into AJAX long polling if you are planing on doing this with just PHP and javascript.  However, there is a better way.  There are a couple of frameworks out there, Node.js being one of the biggest, that will facilitate server push without having to constantly poll the server from every client.  I recomend taking the time to aquaint yourself with Node.js before making any decissions regarding how you plan to tackle this.

Link to comment
Share on other sites

Thanks for the help :) I know this may sound lazy because I don't know alot about node.js, but lets say I don't use long polling and just make an AJAX request every 2 minutes to get new messages for a user, would that have a big impact on the server? I know it's hard to tell because I don't have any details regarding the number of clients or anything like that but what do you think?

 

Thanks,

Adam

Link to comment
Share on other sites

 lets say I don't use long polling and just make an AJAX request every 2 minutes to get new messages for a user, would that have a big impact on the server?

 

Depends, how many users do you expect to have the chat window open at any one time? What kind of server/infrastructure will you be using? For example, a shared server will not be able to support as many users as a dedicated server.

 

But, let's be real here. I don't know what project you are working on. But, you are not going to go out with something and have a milling people logging in the next day. A 2 minute wait will be sufficient for whatever you are building right now and the probably users you will have. If and when it becomes an issue, you can tweak the process to check for updates to be more robust. As long as you write good, modularized code making such an update would be simple.

Link to comment
Share on other sites

web servers can handle several 100's of requests per minute. just using the timer/ajax-request method will work for a casual chat system.

 

you would want to make each request/response as brief as possible and make the server side code as efficient as possible, off loading as much formatting/processing onto the client as possible.

 

the client side request, which should be a GET request btw, would include the id of the last message that has been displayed for that user. the server would just query for and retrieve any new messages with id's greater than that id. at a minimum, the message id column in the database table would be indexed. if there's no new messages, the server should return a simple status value to tell the client side code it doesn't need to do anything, perhaps just an empty json encoded array. if there are new messages, just return the raw message data, leave any formatting/display to the client side code.

 

make sure that the database server has query caching turned on as well. when data in the database table hasn't changed, the same database query being made from multiple clients will return data from the cache rather than retrieving it from the database table. you can have 100's of clients all waiting for a new message and they will keep getting the result from the cache that there's no new messages until there actually is one that was stored into the database table, altering it, which causes the cache to be cleared so that it will then cache the new message(s) for the next series of update requests.

Edited by mac_gyver
Link to comment
Share on other sites

Well I'm sort of working on an instant messaging thing where users can send messages to each other, not just one big chat room if you know what I mean, like Facebook where you can message different people. I mean I haven't written any code for it yet I was just thinking it out to get an idea of how I could do it. I just wanted to make sure I wasn't going to kill the server with Ajax requests :D Also I'll have to look into database caching.

Link to comment
Share on other sites

If you want to go the Node.js route, checkout socket.io, they have demo of a live streaming chat on their homepage

http://socket.io/demos/chat/

 

 

If you want to stick with PHP and jQuery, just create a setInterval script to update your chat box every 10 seconds or so.

<div id="chatContainer"></div>
<script>

setInterval(function(){
    $.post('getChat.php', postData, function(data){
         $("#chatContainer").html(data.msg);
}, 'json');
}, 10000);
</script>
Edited by tampaphp
Link to comment
Share on other sites

  • 8 months later...

node js is an awesome thing for chat systems i developed a facebook like chat application for it at one time messages are instant and you have the client js do the work the server just stores and relays the message

 

I'm looking for similar solution as facebook chat. Can you recommend some tools, open source maybe.

Link to comment
Share on other sites

I'm looking for similar solution as facebook chat. Can you recommend some tools, open source maybe.

 

You have dug up someone else's post from almost a year ago, you would be far better creating your own thread on this that way you will get the update alerts, rather than the OP who I expect is no longer interested in this topic.

 

Please start your own topic and give us some more details to work with - i.e. what languages are you familiar with / looking to use, what setup are you planning to implement, what's the scope of the project etc. Then we will be able to give you a far more complete answer.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.