Okay so I was trying to build something similar to a real time application (something that gets new data from a database as soon as it is added) and the only solution I could come up with was constantly using ajax requests (via the setInterval() method in javascript) to check for new information every couple of seconds, but I was curious about what sort of strain this would put on a database or a server.
Would doing such a thing be overkill or is there a better method for building a real time application?
Your problem boils down to two options,
long pollingorshort polling. In the first option, anAJAXrequest is sent to the server. the server searches for new updates. If found, it returns that immediately. Else it sleeps, say for 0.5 seconds, after which it again searches for updates. This goes on for some prescribed time, after which whatever is there in the data array is sent. On receiving the response, the JS can take appropriate response (if there is some data, perhaps use that to manipulate theDOM), and send another request, and this continues ad infinitum. This has a drawback, that it consumes substantial resources on the server side. But if you want the new updates to reflect almost immediately (like in Facebook), this may be your best bet.In the second option, as you mentioned, the load on the server side is reduced alright. But the time taken to reflect the update may be more. Plus, if the user’s internet speed is slow, it will add to the delay. Not to mention, that repeated requests my waste user bandwidth (although that can be alleviated if you design your site to ensure only minimal data is sent). The Websockets API may be convenient, but to the best of my knowledge, it is not fully supported in all browsers,except their latest versions.