I have a certain concept in mind, but I an unsure what my possibilities are.
User enters a name (html form) on a webpage
Webserver sends push notification to the Android device of the user
Application looks for this name in the contactslist and finds the corresponding phone number
Application instantly sends this information back to the webserver, where the user can use the returned phone number.
The only step I am unsure about is #4. I presume I'd have to use a temporary MYSQL database and use reverse AJAX or something alike to get the returned data to the user without him having to refresh the page (this entire process needs to be completed within a couple of seconds at most - the faster, the better).
This all sounds like a very hack-ish approach to me though, I was hoping any of you could hint me with better ways of tackling this - or maybe a good resource to the approach I described.
It's hard to find solutions when you're not sure what it is you're looking for.
Much obliged!
I can think of two ways of solving #4
poll "did I get the response yet?, did I get the response yet?..."
or use websockets. here's a post I found that might be useful: post
Here's one interesting approach to it all using node.js: nowjs
If you plan to use Androids C2DM, check out the min-max delivery times.
Related
I am working in an Android project designed for doctors. Doctors are required to authenticate when they open the app for the first time.
This authentication process is done through a HTTPS connection, using PHP code in the server-side that returns JSON code to the app, letting it know if the connection has been successful and, if it is the case, it also returns that doctor's list of patients. Let me show a piece of JSON code that would be returned in case of a successful log-in:
Obviously, if the log-in were unsuccessful, the "listOfPatients" attribute would carry no data. After the server generating this JSON code, the app would simply read through it using a JSON Parser.
Now imagine the doctor doesn't have just 3 patients, but 100 patients. And each patient doesn't have just 3 attributes ("Age", "Phone", "Smoker") but dozens of them appropriately nested where required. We would then have a somewhat large (but maybe not too complex) JSON code to read through.
In this project I am designing the Client code (i.e. the Android App), whereas the Server code is written by other guy. He is asking me how I'd like the server code to be written in order to facilitate the "Android Client - Server" interaction and achieve the best, smoothest user experience possible.
I answered (this is the really-short version of my answer; don't concern about the server-side-code security since it is not the goal of this question):
Create a login.php that allows for POSTS queries. My App would send "user" and "password" and the server would compare it with the database.
The server would then generate appropriate JSON code depending on the success of the doctor's log-in request.
The Android app would simply parse this JSON and display it to the user in form of list-views, and so on (the way I display this data to the doctor does not matter here in this question).
I was wondering two things:
Knowing that the JSON will contain hundreds of attributes, how efficient is this code? Is there a better way to achieve this functionality? How would you do it?
The vast majority of these attributes' values will change on a daily basis (for example, "bodyTemperature" or "bloodPressure"). Furthermore there will be "importantNotifications", where patients would notify their doctors in case of an emergency situation. I don't think it would be efficient to go through the entire process ("server create JSON ==> client read JSON ==> client display JSON") over and over again, minute after minute, hour after hour, day after day. There must be a better way to do it (maybe local storage? I would then have to discern which attributes to read only once a year ("age"), once a month ("phone"), once a day ("bodyTemperature") or every 30 minutes ("importantNotifications"); How could I then discriminate which values I'd need to read from the JSON in each session?)
Now you will be likely using GSON to parse the response from the server. Also you might define default values and tell the server not to return anything that is equal to default values like smoker - default NO (minimize the ammount of data to transfer). You are highly likely to display the patients in a ListView or RecyclerView.. Google a bit how to implement lazy loaders, meaning you tell the server to return just a few results, not all and when the user scrolls to end, you ask the server to give you more if there are any.
Also using caches on Android is a great way to save a couple of unnecessary requests to the server. You define of how long a cache is valid, say 5 mins and if you want to repopulate a list, check if it's still valid. But you should always leave a manual refresh option, SwipeToRefresh is a great and simple way to do just that.
Hope somebody else can have something more as I am interested in this also.
I think it is inefficient to do a request on each page visit to collect the information. I would like to know what the theory of best practises are regarding to working with data that is received from an API.
Should the data be stored in my own database or should I just make a call to the API every time?
As example if I would use Strava to get my running results. Should I just use the result directly from the API or should I store these in a database. And than check after a certain period if there are new or updated results and update my database accordingly?
I have read some articles about RESTful API's but they only give information about how to get, delete, update the data etc.
It depends on what you are using it for...
If the information is likely not to change and is likely to be reaccessed on your site then store it.
Otherwise just get it from the API, it will always be up to date and it's less code to write if you don't have to store it.
If you are storing it and you know what information you are likely to require you could retrieve in the background at set-intervals.
You can also look at other factors when making your judgement, such as the speed of the API or if you have a API call limit.
You are probably not finding an definitive answer as there isn't one, but I hope this clarifies this for you =]
I don't know of Strava or whatever API you will use.
In my case, generally in applications there is a need to keep track of what have been exchanged with an external system, for example to give proof that the data has been read or written.
For this, usually there is something kept in the local database, and usually it does not have to be the track of the complete data exchange.
I need some advice on website design.
Lets take example of twitter for my question. Lets say I am making twitter. Now on the home_page.php ,I need both, Data about tweets (Tweet id , who tweeted , tweet time etc. etc) and Data about the user( userId , username , user profile pic).
Now to display all this, I have two option in mind..
1) Making separate php files like tweets.php and userDetails.php. By using AJAX queries, I can get the data on the home_page.php.
2) Adding all the php code (connecting to db, fetching data ) in the home_page.php itself.
In option one, I need to make many HTTP requests, which (i think) will be load to the network. So it might slow down the website.
But option two, I will have a defined REST API. Which will be good of adding more features in the future.
Please give me some advice on picking the best. Also I am still a learner, so if there are more options of implementing this, please share.
In number 1 you're reliant on java-script which doesn't follow progressive enhancement or graceful degradation; if a user doesn't have JS they will see zero content which is obviously bad.
Split your code into manageable php files to make it easier to read and require them all in one main php file; this wont take any extra http requests because all the includes are done server side and 1 page is sent back.
You can add additional javascript to grab more "tweets" like twitter does, but dont make the main functionality rely on javascript.
Don't think of PHP applications as a collection of PHP files that map to different URLs. A single PHP file should handle all your requests and include functionality as needed.
In network programming, it's usually good to minimize the number of network requests, because each request introduces an overhead beyond the time it takes for the raw data to be transmitted (due to protocol-specific information being transmitted and the time it takes to establish a connection for example).
Don't rely on JavaScript. JavaScript can be used for usability enhancements, but must not be used to provide essential functionality of your application.
Adding to Kiee's answer:
It can also depend on the size of your content. If your tweets and user info is very large, the response the single PHP file will take considerable time to prepare and deliver. Then you should go for a "minimal viable response" (i.e. last 10 tweets + 10 most popular users, or similar).
But what you definitely will have to do: create an API to bring your page to life. No matter which approach you will use...
Currently I'm making a chat application where only admin and users chat, no user-to-user chat . The design is: every chat is stored in database and each 2 seconds user and admin make an AJAX request (to a php file) to see if there is a new chat dialogue, and if there is, pull the data into the textbox. It all seems normal and working good.
Problem is as more user is talking to admin at the same time the AJAX request is becoming a lot, and by testing, the web performance already decreased with only 5 users chatting at the same time. And the input is slow too, every time user press enter they got to enter the data into database first before the admin can read it (and vice versa).
I have been told that using JSON is a recommended way, but I have no idea how to do it, can someone please at least tell me how's the design or flow is going to be if use JSON? Or is there a better way to make it? (by the way, using node.js is currently impossible for my current hosting, so don't put it in suggestion lists, sucks I know).
You should change the AJAX responder phps output to JSON. (you can use the json_encode php function for example.) And you should parse(eval) this in javascript.
I am a bit sceptic. It think It could reduce the network usage by more than 50%.
Maybe you can try a message queue, like 0mq or rabbitmq.
There are a lot of chat examples around.
I need a kayak.com like, functionality.
That is, the user enters a keyword and I will need to display results as the become available. The important thing is that data should be displayed AS it BECOMES available. Kind of progressive display? Don't know if this is right term.
Kayak.com displays or gives the impression that data is displayed as they become available after an asynchronous call.
Can anyone give directions on this topic? (php on the server side) Is this a case of PUSH ?
Thank you.
Push is a nice way of keeping your clients up to date, however, the more users the push server is pushing data to, the more resource intensive it gets. If this is an application where you expect to have multiple users, pushing may be a bit intensive for your needs, whereas polling regularly might be better. But give push a try, these guys (icefaces) have a nice ajax push implementation.
Hope this helps!
It seems that the server is sending its response in paginated sections. This enables the client side to begin rendering much sooner. After the client receives the first page, it renders it and begins the request for the subsequent page, and so on, until there are no more pages remaining.
You may also be interested in reading up on Comet which can be thought of as an "AJAX Push". Not sure on any PHP implementations but I know there are several other solutions out there which you could potentially tie PHP into.
I've thought about this before but never really built anything. My thought process on this was to have your script grab the latest record and get some kind of unique identifier (ID or date). You log this id with javascript and have a request try to get results that are newer than the current ID every second (or longer if you don't need the data instantly). If results are returned, you once again log the latest result for the next request.