Is running an ajax request every second bad? - ajax

I am currently trying to create a chat function and notifications function on my site that would get the current users logged in and notifications periodically from a mysql database and right now I have it set to do an ajax call every second to a php page which returns the users online and notifications back in a json object. I am just trying to make sure there isn't a better way of doing this that I can't think of. Also is doing an ajax call to my server so often bad? I would really appreciate any insight on this. Thanks in advance!

As long as you have a hand full of chatters: no. It is not the best design, but it will do the trick. If you are looking for scalable techniques look for Comet or Web Sockets.

Related

Sending live data to a client: Are websockets the best option (Vuex & Socket.io?

I'm trying to set up a front end UI for the webApp I've been working on and have a clarification question before proceeding further.
Right now I have multiple sensor units streaming data to the server that is saving it to a database and then set up a crude api, to be expanded on later, to interface with the server and DB.
Now, moving to the front end, I feel like I'm bashing my heading first trying to learn react and now vue. The first feature of the UI I'd like to have is to constantly see the sensor data displayed in a table on the browser.
Should I be calling this data from the server using the API http requests or have it sent using websockets?
After spending a few hours trying to get vue connected to the socket opened on the server I wanted to check if I'm just looking in the wrong direction to begin with?
Thanks!
It would be best to create a websocket connections since your data is continuously changing. You are definitely on the right track.
Just create an vuex action that will commit your data to the store. It does't really matter which websocket tool you use. Most of them work quite the same. There will be most likely a callback function which gets triggered when new data has been sent to the front-end. This is where you would call your vuex action and pass through the data. If you can edit your question with some sample code, I can help you in more detail.
Just to some it up:
1) Open Websocket connection to backend service
2) Create action which would save the data to store via mutations
3) Specify websocket callback function to call action

Phonegap and Django Authentication

I am in the middle of building a PhoneGap (Cordova) app which I would like to be able to talk to a Django site of mine. The steps needed to get the app working are:
Authenticate the user (stay logged-in across app restarts) (e.g. get session cookie from Django for communication with the service - where to store?). Note: The Django endpoint uses https.
When app receives push notification load some data from my django site.
Make selection on data and submit response back to my django site (will need the csrf token?)
I was able to sort out the push notifications but now I am wondering which solution would work best for the communication with Django.
As I understand there are two possible approaches:
Either to implement a REST service with something like tastypie or
try to setup the communication via ajax (e.g. jQuery)
At the moment I am thinking that going simply ajax might be the best approach since the app is fairly small and there are no additional requirement for a REST API.
It would be great if anyone could give me any pointers on how to solve this or share some experiece / code. Especially the steps of the authentication process are unclear to me.
I am not sure if this is still an open question but it is sure an interesting one.
I would strongly suggest on using the django-tastypie and you could start by using the docs which are indeed a great point of reference.
My experience until now has shown that I should always start by making my api clear(and rest) than choosing an easier faster solution(e.g. ajax) because if your app is a successful one, frameworks like tastypie help you scale.
The authentication process is pretty straightforward if you choose the basic one.
You just ask for the user credentials and there are many clients implementing the client side basic auth.
Fortunately, tastypie supports more than this. For example, the api authentication and you could read more here.
If you need anything else, please let me know.
Regards,
Michael.

Ajax Server-Side synchronized HTTP request?

I am building a website which has a cron job that generates a file to the hard drive regularly. However, the timing of this generation is not precise and I would like it to be loaded as soon as it is generated by the browsers of my visitors.
Is there a nice way to make my server notify my visitor's browser to reload the page?
Other way around is quite heavy :(
Thanks!!
you should read about Comet, Comet allows you the receive "Push" from server-side to your client-side.
Eg,. thats the way how facebook chat works..
Take a look,
http://en.wikipedia.org/wiki/Comet_(programming)

Live chat using AJAX

Hello everyone
I just wanted to know if I use AJAX for my chat engine, will it crash my server or it will work fine for a traffic of about 1500 people at a time ??
Also suggest me some way to make automatic ajax request to update the contents of chat box. How much delay between consecutive query should be set so that it doesn't cause my server to overload ???
Thanks in advance :) :)
Depending on your requirements, you might want to look into WebSockets. At a local hacknight, we produced a simple chat system pretty quickly using http://faye.jcoglan.com/. It's a pub sub service that you can use javascript on the client side to subscribe to a specific channel. Then it's just a matter of coding the front end to work with the input and output.

Is it possible to send info from a webpage to a server without reloading?

I have found very little on this topic. I'm trying to work out a way to synchronize pages cross-web without having to constantly reload pages to get new information, since the rate at which this would be necessary would cause the page to be outrageously slow.
The flow I'm thinking is this:
User A alters info displayed on Page A.
Page A sends info to server.
Page B checks server for new info every 10ms or 100ms.
Page B loads Page A's new info.
I can see AJAX as being sufficiently fast to retrieve info from the server, but have found no way to send data to a server without having to refresh every 10ms, which, even using an iframe to avoid reloading the whole page, seems far too slow to me. Correct me if I'm wrong.
So my question is, is there any way of which I am unaware to do what I am attempting? I have seen methods involving a Java server applet, but that's a bit above my head at the moment. If that's the only way, I'll learn it, but I'd love to avoid that if possible.
There are two possible interpretations of what you wrote, the first which seems to be what you've actually said is that you want to know how to send data with an Ajax request, the second is that you want to know how to push unsolicited data from the server to the client.
Ajax can easily add data to a request it makes - just add query-string parameters, or make a POST request and use XHR's send method
Use comet - i.e. keep open a long-lived connection and send data only when there is something to send.
One of the possible way to implement what you want is to use Comet technology. For example - facebook uses it to interact with their servers.
If you are retrieving info fast using AJAX, then you are also sending info fast with AJAX...
GET requests are still telling the server something. For example, lookup RESTful web-services.
You could use updater of Prototype.

Resources