| |
|
Welcome to KnowledgeSutra - Dear Guest | |
How Can I Make An Auto-updating Shoutbox Type Script?
Started by shadowx, Aug 23 2007 02:24 PM
11 replies to this topic
#1
Posted 23 August 2007 - 02:24 PM
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?
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?
#2
Posted 23 August 2007 - 05:11 PM
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.
#4
Posted 23 August 2007 - 05:41 PM
shadowx, on Aug 23 2007, 01:29 PM, said:
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?
#7
Posted 08 December 2008 - 12:55 PM
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).
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).
#9
Posted 12 October 2009 - 02:54 PM
Check out my script www.ndvf.tk
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 www.ndvf.agilityhoster.com/chat/chat.js
It is starting with that comment //startuvanje na chatot or it was //starting chat
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 www.ndvf.agilityhoster.com/chat/chat.js
It is starting with that comment //startuvanje na chatot or it was //starting chat
Edited by TheDarkHacker, 12 October 2009 - 02:55 PM.
#10
Posted 12 October 2009 - 07:12 PM
@SofiaComp
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
), 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 ??
Quote
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
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
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 ??
Reply to this topic

1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users















