Jump to content

php and ajax - websockets vs ajax


PNewCode

Recommended Posts

Hello everyone. I have a chat that I made. The problem is that it crashes every 10-50 min (depending on how many people are in there).

I know having it refresh with ajax every second isn't the smartest way but could that be the reason? The error is "out of memory". Everything that I'm reading says it's a browser thing but every single viewer is getting the exact same thing on all browsers (sometimes worded a little different on some browsers)
Chrome, Firefox, Edge, Android default, and Safari all do this.

I don't know a thing about websockets, but it looks like this could be preventing that. Is that true?

I cringe at having to learn all that stuff with Node.js and stuff it looks like this will require. So I'm hoping maybe there's a diffent solution

 

Note: I didn't include any code here because it's a general question really. And my chat is made up of 4 different files for customization and stuff. It is using php mysql database and ajax every 1.5 second to load the chat to prevent lag from entering a post and appearing in the chat window.

 

All advice is appreciated.

Note 2: If you're wondering "Why are you using a database and ajax" the answer is, because thats at the extent that I've learned so far.

 

Edit: I forgot to add that there are no errors showing or warnings in the console or logs, not even suggestions

Edited by PNewCode
Link to comment
Share on other sites

  • PNewCode changed the title to php and ajax - websockets vs ajax

where is the error being generated at or seen at?

i suspect is it a php error message being displayed in the 'chat' output? the reason it may appear differently is because it contains html markup, which may or may not get rendered by the browser.

out of memory errors as usually caused by logic that either has an initial condition problem, where an assumption being made isn't satisfied, or a loop that runs forever and keeps adding data until all memory is consumed, without removing older data.

Link to comment
Share on other sites

Making an ajax call every 1-2 seconds wouldn't cause what you're seeing, but if you're appending the full dataset into a JavaScript array on every ajax return that could be it. If you initialize the page with a JavaScript array containing all messages and then append all messages again every 1-2 seconds, that array is going to grow exponentially very quickly and an out of memory error would be a distinct possibility.

Make sure you're returning new messages only from the PHP so only those get appended to the array or object.

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.