Jump to content
xisto Community
Sign in to follow this  
shadowx

How Can I Make An Auto-updating Shoutbox Type Script? I have a few ideas involving AJAX but im looking for something more..

Recommended Posts

Hi all.Ive just started to experiment with AJAX working with PHP and MYSQL to get data and display it and im looking to implement it into a shoutbox similar to the one here on the forums but i have a question...i know how to use ajax and JS to refresh the contents of a DIV every few seconds r so and that essentially reloads the shoutbox content as the REFRESH button does up there ^^ on the shoutbox but i think it silly that it should update every few seconds even if theres no new activity so what im looking for is a way of letting my AJAX know when there is a new entry in the DB and then reloading the DIV to show it as a shout. One way i just thought of is to use PHP to detect if a new entry has been made, if it has then i will echo out a variable containing the complete entry and if nothing has happened then i can echo out a variable that basically says "no" and have my AJAX detect that and not change the DIV. the downside is with all this AJAX loading the php file every few seconds server load wouldnt be good and it also has a habit of slowing down the browser and lagging it every now and then, this leads me to the next question..Is there a way to notify my AJAX via php and SQL of a new entry in the database without loading the php page, so instead of AJAX contacting the PHP it works the other way around?

Share this post


Link to post
Share on other sites

In order to have the PHP script run, it has to be first called, of course. And in order for the AJAX to pick it up, it would have to be running at some kind of interval (of which the PHP script would be runned via cronjobs, whether or not there are visitors) to check the PHP script. This would not only put (extra) load on the server, it would also put load on the browser (however little it may be) of the user. On the version of my shoutbox that i'm working on (it has AJAX), i don't have it refresh at certain intervals, i just let the user click the "Refresh" link if they want to; the shoutbox will reload if they refresh the page or move to another page on my site. So i don't think what you want is possible: having PHP contact AJAX; for in the end it'll just be AJAX contacting PHP anyways and not the other way around.

Share this post


Link to post
Share on other sites

In order to have the PHP script run, it has to be first called, of course.

True, i didnt think of that...

I was wondering how the T17 shoutbox works, is that interval based? Eg a user shouts a message and so many seconds later it is updated?

Share this post


Link to post
Share on other sites

I was wondering how the T17 shoutbox works, is that interval based? Eg a user shouts a message and so many seconds later it is updated?

The way i've seen it work is it refreshes at an interval and it refreshes after the user submits a shout; the interval and shout times don't affect each other.

Share this post


Link to post
Share on other sites

Fair enough! It seemed to me as though it only updated as a shout was made but of course that was probably just because a user shouted before the update. Ah well i shall use my interval option then!thanks

Share this post


Link to post
Share on other sites

If you're making a shoutbox, then you should make it so that when the entire box doesn't actually refresh, but instead it loads newer messages and some javascript should add them to the box. That way, you don't load the newest 10 (or whatever) messages every 5 seconds, and instead only use a few bytes of bandwidth (excluding headers and stuff :)) You could do this by sending a variable signifying the latest shout received, and then returning newer ones.It could also help if when there are no new messages, the delay is increased, and when a message is sent or received, the delay is reset. Alternatively, you could do something like done here and disable the shoutbox after a period of inactivity (it's 10 minutes here).

Share this post


Link to post
Share on other sites

Check out my script http://forums.xisto.com/no_longer_exists/
That chat is mine using AJAX and PHP.You don't need to store messages in MYSQL,its more complicate that way,you could simple use one text file.AJAX request function should be called every 20-50 miliseconds...I used setTimeout("call function blabla",50); and the function will get responseText from the php page which gets response from the text file.It's small comlicated and normally for advanced users.I am using the ajax library instead of xml http request.Ps: You can see my script from http://forums.xisto.com/no_longer_exists/ It is starting with that comment //startuvanje na chatot or it was //starting chat

Edited by TheDarkHacker (see edit history)

Share this post


Link to post
Share on other sites

@SofiaComp

i think that the user should make an http request to the server when he clicks enter or shout.

why you need auto-update anyway?

 

ps. i know how to use javascript but am not familiar with ajax


Let's suppose you are using the shoutbox; since there can be many users using shoutbox and not only you, you will want to know the messages sent by the others even when you are not sending any message. In this case you need the auto-update.

 

@shadowx, nabb

 

I have heard a lot about shoutbox feature, but It never occurred to me that it should run this way. It is very interesting to know this alternative viewpoint.

 

My solution (I do not know if it really would work :P), quite similar to Nabb :

 

In Ajax, we have two methods:

1. First method, which sends a request to the server.

2. Second method, which is called when a response is recieved from the sever for a request made to server.

As, it is an asynchronous call it is not necessary that second method will get called immediately.

 

What we can do is :

1. when a request to the server to fetch a new message is made, then it(server) should put the request in wait/queue mode and should not return the response until and unless there actually is a new message.

 

2. When a new message arrives then server should return response for all the pending requests which will get caught by the second method. Second method then should refresh the client window(or div) and then call the first method again.

 

I am not sure if 1. and 2. can happen, especially the 2; but I think in java we can use a single servlet that handles the request and use wait and notify method calls to get it done.

 

Earlier, when we didn't have the ajax; remember, we used to chat on yahoo using the java applet. I don't know how they used to do it, but if it can bring down the server load then why can't we build a shoutbox as java applet ... just a thought ??

Share this post


Link to post
Share on other sites

two options you can change it to setInterval('updateShoutbox()', 4000) and it will run ever 4 seconds or you can just include another setTimeout('updateShoutbox()', 4000) in the updateShoutbox() function itself-- Just before the closing bracket-- so it will call itself every 4 seconds function updateShoutbox(){ //send the post to shoutbox.php $.ajax({ type: "POST", url: "shoutbox.php", data: "action=update", complete: function(data){ loading.remove(); messageList.html(data.responseText); } }); setTimeout('updateShoutbox()', 4000); }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.