Jump to content

Recommended Posts

Ok Im building a live web chat using php, mysql, javascript, ajax

 

I am fine with sending and retreiving data in real time using ajax as the transfer protocol

 

However, my problem, which is becoming a nightmare, is how to I display it !!

 

Lets say the below is the chat screen,

 

<div id="messages">

Jamie - Hello

Peter = Hi

Jamie = Nice Day

Peter = I know

</div>

 

Lets say Jamie's next message is "What are you doing today". I can send that off to the database no problem and I can get peters live chat to pull that message in, however, how do I get it to display underneath Peter = I know

 

As a practice I was using

 

document.getelementbyid('messages').innerHTML = document.getelementbyid('messages').innerHTML + "<BR>What are you doing today"

 

That works, but, lets just say the chat gets to 1000 lines, for each new message those 1000 lines have to be pulled out via javascript, a new line written to it and then printed back in the div

 

Im not sure but I'd take a good guess at this not being optimal and will cause CPU load on the users machine

 

What is the best way to do this! Chat messages are deleted after 1 minute, but that doesnt matter because by then they have already been sent the the users chat. Just incase you are thinking of completely repopulating there chat with every message they have sent since the chat started..

 

So basically, only messages sent within the last 5 seconds are picked out from the database and sent back to them..

 

Any help?! Really stuck!

Link to comment
https://forums.phpfreaks.com/topic/75144-solved-populating-fresh-data/
Share on other sites

I dont think there are any options other than the two you've mentioned. You either do it via javascript, or by refreshing the whole chat window. I don't suppose either will cause a huge load - be realistic, you're not going to get 1000 lines of chat in 1 minute.

Really? I have seen quite a few ajax driven chatrooms surely they dont use the same method?

 

1: get javascript to pull in every single message

2: write the new one

3: push it all back inside a div

 

?

 

I dont mean 1000 lines a minute, I mean, if they are chatting for an hour the accumulitive messages will be 1000+

 

At that stage, every message sent has to to the 3 stages above

 

There has to be another solution and leave old message on the page, untouched and simply write the latest ones at the bottom

Not that i can think of. At the end of the day, you still have to do something with the old data, otherwise you'd end up just writing on top of all the old chat.

 

Personally, i'd favour the option of reloading the whole chat - i could be wrong, but i think you'll find a lot of AJAX driven chats do the same. You will notice that if the chat stays idle, e.g. no one talks, the chat window will empty. That way, there's never huge amounts of data to be dealing with.

If someone could advise me on how well javascript can deal with holding and pushing large sets of data

 

for instance

 

1000 x Hello my name is steven blah blah blah its a nice day

 

 

How easy could javascript read 1000 lines, add a new line and then push it all back inside a div

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.