IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

How Can I Make An Auto-updating Shoutbox Type Script?

, I have a few ideas involving AJAX but im looking for something more..


shadowx
no avatar
Look around, what do you see? Incorrect.
***********
Group: [HOSTED]
Posts: 1,347
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719
Spam Patrol
myCENT:79.30



Post #1 post Aug 23 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?
Go to the top of the page
+Quote Post
truefusion
no avatar
Do you want to know?
****************
Group: [MODERATOR]
Posts: 2,725
Joined: 22-June 05
From: 127.0.0.1 — no place like it.
Member No.: 8,528
myCENT:40.50



Post #2 post Aug 23 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.
Go to the top of the page
+Quote Post
shadowx
no avatar
Look around, what do you see? Incorrect.
***********
Group: [HOSTED]
Posts: 1,347
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719
Spam Patrol
myCENT:79.30



Post #3 post Aug 23 2007, 05:29 PM
QUOTE
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?
Go to the top of the page
+Quote Post
truefusion
no avatar
Do you want to know?
****************
Group: [MODERATOR]
Posts: 2,725
Joined: 22-June 05
From: 127.0.0.1 — no place like it.
Member No.: 8,528
myCENT:40.50



Post #4 post Aug 23 2007, 05:41 PM
QUOTE(shadowx @ Aug 23 2007, 01:29 PM) [snapback]342285[/snapback]
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.
Go to the top of the page
+Quote Post
shadowx
no avatar
Look around, what do you see? Incorrect.
***********
Group: [HOSTED]
Posts: 1,347
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719
Spam Patrol
myCENT:79.30



Post #5 post Aug 23 2007, 08:23 PM
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
Go to the top of the page
+Quote Post
Variablez
no avatar
Member [Level 2]
*****
Group: [HOSTED]
Posts: 87
Joined: 13-October 08
Member No.: 69,638
myCENT:98.49



Post #6 post Dec 8 2008, 12:11 PM
I would like this too for my site, welp back to searching!
Go to the top of the page
+Quote Post
Nabb
no avatar
Super Member
*********
Group: [HOSTED]
Posts: 244
Joined: 24-July 08
From: Australia
Member No.: 65,436
Spam Patrol
myCENT:58.68



Post #7 post Dec 8 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 tongue.gif) 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).
Go to the top of the page
+Quote Post
SofiaComp
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 23
Joined: 3-February 09
Member No.: 76,506
myCENT:94.17



Post #8 post Feb 5 2009, 08:53 PM
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

This post has been edited by SofiaComp: Feb 5 2009, 08:54 PM
Go to the top of the page
+Quote Post
TheDarkHacker
no avatar
Super Member
*********
Group: Members
Posts: 203
Joined: 6-October 09
From: somewhere where arent others
Member No.: 89,308
myCENT:27.27



Post #9 post Oct 12 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

This post has been edited by TheDarkHacker: Oct 12 2009, 02:55 PM
Go to the top of the page
+Quote Post
manish-mohania
no avatar
Newbie [Level 2]
**
Group: Members
Posts: 39
Joined: 5-September 09
Member No.: 87,644
myCENT:9.29



Post #10 post Oct 12 2009, 07:12 PM
@SofiaComp
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


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 tongue.gif), 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 ??
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   5 Shibbeh 16,130 20th August 2004 - 10:04 PM
Last post by: ill
No New Posts   2 icleric 3,673 4th August 2004 - 08:48 PM
Last post by: icleric
No new   16 -gladiator_us- 21,924 24th August 2009 - 11:05 AM
Last post by: jamjamnorman
No New Posts   7 SilverBoarder 14,282 9th February 2009 - 11:02 AM
Last post by: aloKNsh
No New Posts 6 LuciferStar 14,683 19th September 2009 - 08:57 AM
Last post by: akira550
No New Posts   4 xmae 9,160 24th August 2006 - 03:21 PM
Last post by: juice
No new   14 Raptrex 17,526 8th December 2008 - 01:40 PM
Last post by: carToon
No New Posts   7 football123213 15,612 20th August 2004 - 12:25 AM
Last post by: ill
No new   15 -prodigy- 14,180 27th February 2005 - 10:22 PM
Last post by: alexia
No New Posts   2 -Pandemonium- 12,093 22nd August 2004 - 04:25 AM
Last post by: -Pandemonium-
No New Posts   6 -prodigy- 4,919 23rd August 2004 - 08:23 PM
Last post by: -prodigy-
No New Posts   8 -Pandemonium- 9,330 25th August 2004 - 04:00 PM
Last post by: -Pandemonium-
No New Posts 7 arash 10,440 16th May 2009 - 04:13 AM
Last post by: k_nitin_r
No New Posts   0 Raptrex 7,129 6th September 2004 - 11:19 PM
Last post by: Raptrex
No New Posts   6 Lunaray 9,438 10th September 2004 - 08:43 AM
Last post by: Lunaray


 



RSS Open Discussion Time is now: 21st November 2009 - 07:21 AM

Web Hosting Powered by ComputingHost.com.