As a personal project, I'm making an AJAX chatroom application using XML as a server-side storage, and JSON for client-side processing.
Here's how it works:
AJAX Request gets sent to PHP using GET (chat messages/logins/logouts)
PHP fetches/modifies the XML file on the server
PHP encodes the XML into JSON, and sends back JSON response
Javascript handles JSON information (chat messages/logins/logouts)
I want to eventually make this a larger-scale chatroom application. Therefore, I want to make sure it's fast and efficient.
Was this a bad design choice? In this case, is switching between XML and JSON ok, or is there a better way?
EDIT:
Two mechanisms prevent a big server load when fetching information from the server:
An "event id" is assigned to each message/login/logout, so all that's sent back to the user is the events his client hasn't yet processed.
When an XML file becomes too big, a new one is created.
As far as I am concerned, JSON is always a good choice for async. data transfer, because it is not as bloated as XML is. I'd choose latter only if I want the data to be human readable, e.g. config files.
--- Edited:
And remember: Serializing/deserializing XML is a performance issue and not particularly convenient for persisting web application data with high frequency access, while, as mentioned, using xml as config files is imo best practice.
Both XML and JSON are good for inter-applications communication. In Javascript, JSON is much easier than XML to deal with, so that's what I'd recommend.
As for storage... both are terrible as large datastores I'm afraid. MySQL would work better than editing a file, but it's still not an appropriate solution for a chat, especially if you're on a shared host. You may want to take a look at SQLite, perhaps creating one file per chat room.
Related
Ok I found a few questions on how to get data from a MYSql database into an iOS app, but I am just asking a few best practices here. I know these can all be separate questions, but I am hoping that they can be answered in a way that they relate to each other.
Am I correct to understand that to be able to get data into an iOS app - I need to first generate a JSON file, have that stored on a server and than have the app download this file??
If the previous answer is NO then does that mean, I can pull in data on the fly?
Lastly I have seen PHP examples to create JSON files, but iOS is in Objective-c. Does this mean I need to load a UIWebView to be able to load the PHP page that generates the file?
What I have:
I have a MYSql database - it is set up through PHPMyAdmin, so I am not familiar enough with the creation process of the database yet. I will look into that.
I can also export the JSON file from PHPMyAdmin, but that is no good to me in a iOS app.
I also have the parsing from a JSON file into an iOS app sorted, but I want to be able to do this on the fly instead of creating potentially hunderds of files.
I hope someone can help me here:-)
I am not necessarily asking for code, but would be mad to ignore it:-)
The problem is that there are not any iOS libraries for directly connecting to a MySQL server; and you really wouldn't want to do that, anyway. So, you need an intermediary server capable of sending data in a format your iOS application can understand. Note, this does not mean the data has to be JSON formatted. But it is very easy to use JSON as the format for your data. Most languages have native support for generating JSON from its native object format(s).
Once you have a server capable of sending data in your preferred format, you need to write some way for your iOS application to retrieve it. You do not have to use a UIWebView for this. As mentioned, the NSURLConnection framework is very easy to use to make such a request. However, there are a lot of other factors to consider when making network requests and others have already done most of the work for you. I like using the AFNetworking framework in conjunction with JSONKit. AFNetworking makes asynchronous calls to remote web services very easy, and JSONKit is nicer than NSJSONSerialization in my opinion.
What I do to retrieve data from MySQL to my iOS app is:
Create a PHP file on your server and prepare it for GET methods (you're going to send data from the iOS app)
Send a request from your iOS app to your php file, like: "www.yourdomain.com/data.php?name=..."
Process the information on your php file and echo the json output.
When connectionDidFinishLoading: convert the NSData to an Array using NSJSONSerialization.
Do whatever you like with the output information
That's just do way I do. I'm not familiar with other approaches.
PHP (and any other server side language) can take the data from the MySQL database and output it to any client as JSON, on the fly. No need to save the JSON to disk beforehand. Of course, from the client's point of view, there really is no fundamental difference (except the data will always be the latest representation of what's in the database).
You also don't have to use a UIWebView. There's a number of ways to make an HTTP request using Objective-C, but you'll likely want to look at something along the lines of NSURLConnection's sendSynchronousRequest:returningResponse:error: method (I prefer using synch methods inside an async block, but that's not the only way). You can find many tutorials on how to do similar things, as well as higher level libraries to simplify the process.
Right now I'm in the middle of a project at college, regarding IPhone-Development.
The project consists of a webserver with mysql database and multiple clients (IPhone Devices).
I'm struggling a bit with the basic cocept, which is the communication (receiving, sending data) between IPhone and Webserver.
I've set up an Webserver with a MYSQL-Database. I also have a PHP-Script that accesses the DB and writes the db-Data into an XML-File.
1)What would be the best way to proceed with the Client (IPhone)?
2)Is it possible to directly access a .php File and download the XML to the IPhone?
3) What will be downloaded to the IPhone? The whole XML-File or the XML-Content?
I would then go on to process the XML-File with a XML-Parser (preferably NSXMLParser).
4) How is it possible to store the received data persistently on the IPhone? NSMutableArray? This feature is required for offline mode if any data is added on the IPhone-device.
This brings me to my last question:
5) How do I send data back to the webserver? Or to be more specific, wich datastructure do I have to use to send data back to the webserver? What would be a reasonable way to do this? For example: creating a new XML-file and sending it back to the webserver?
Best Regards,
Alex
Must the data protocol be XML? I'd opt for JSON as the data protocol!
If it's feasible for you to use JSON then take a look at the lightweight SBJson framework. It's going to be much easier to use JSON over XML for iOS, believe me!
To summarize on the other questions, regarding server communication etc.!
Is it a mandatory precondition for you to write the networking code from scratch or are you allowed to use existing open frameworks for that? If you may use a framework then f.e. take a look at MKNetworkKit! It'll take away almost all the hassles that come with writing or incorporating own network code and it has JSON serialization/deserialization build in already!
If not then go with NSURLConnection, NSURLResponse and probably some NSOperation/NSOperationQueue for asynchronous handling and have some fun! :-)
And don't forget the Reachability.h/m from Apple! ;-)
For data persistence on the device either go with CoreData, which is very powerful but mybe (regarding complexity) to much of overhead for your purpose. Or just store your stuff with/in NSUserDefaults!
I'm pulling data from the Instagram API and caching it using php for use with a Polymaps application. My question is simple (but probably not simple) -- I've been approaching this from the standpoint that I should format my data in php and then make a json file I can use directly with polymaps.
Would it be better to just cache the json file and use javascript to pull the information I need from it?
Or is this mostly a matter of personal preference?
You should always favor client-side processing in these cases. Server-side processing tends to add up to server load. And it really depends on:
whether JS is a must for your application (in this case, it is; because without javascript those maps are dead anyway and so it will be the processing )
whether the processing mechanism is not hard to replicate and is not a company secret (this is up to you)
I am about to write a script that links multiple databases across servers in php, and I am trying to decide the best tack to take. I like my DOMDocuments, and already have multiple xml feeds coming from the database that the others can hook into. On the other hand the database it fed by a form, and this could easily send its data across the other datbases on submit using Curl or even jsonp.
My personal feeling is parse the xml on a daily basis, but the other approach would make the db linkup more instantanious.
What do you guys think?
I think its better you use something like JSON with less oerhead. With XML you have a lot of overhead.
Or you use a socket and send the data direct over connection with your own short protocol. I think XML is ok but for linking server is JSON or a Socket really better.
I think we would definitely need some more information here before we can give you solid advice.
But I definitely lean toward the form route, of the two options you've outlined, for sure.
On the one hand, you said the xml feeds already exist. If they contain information other than just the changeset, then that balloons the amount of data you have to process on the receiving end.
On the other hand, you've already written them, so there may be less work involved. You have to write the "receiving" code either way.
I'm designing some UI's for a product and we want to utilize jQuery and PHP. The content to generate is a list of checkboxes (10-100) that a user will be modifying (removing multiple at a time and changing the entries). I thought I'd try something new and ask StackOverflow (read: you) what is preferred: generate the html in the php call and return, or return JSON data that jQuery can than go and generate the html checkboxes using.
I appreciate the feedback! My preferred method so far is to let PHP generate html because it knows more about the data at the time of modification (it's interacting with the database and it could build the html easy enough without having to pass back id's, names, etc in JSON).
Thanks!
[Edit] Bandwidth is not a constraint. This is an internal intranet application. The information needing to be printed to the user will not require dom modification after the fact (outside of checkboxes, but that's built in to the browser...) some good points have been made on the amount of data that's being passed back though:
passing back
Label
vs.
{
"Label": "Unique_ID"
}
is obviously a lot of redundancy.
There's really no right/wrong way to do this. Passing JSON back, and then using client-site processing to turn that into HTML uses less bandwidth, but more local processing power. Passing HTML back, uses more bandwidth and less local processing (these are seriously minor points, only if you're talking extremely popular or frequently changing sites might it even be relevant).
Return Flexibility - HTML
One of the benefits to HTML passing is you can return anything if the request causes an error, or could generate different types of data you just return different HTML. If you're returning JSON, the parsing script has to deal with these alternate structures (ie error handling, and/or multiple structure parsing algorithms).
Local Processing - JSON
If you're localizing, sorting, or framing the data from the user's point of view, it may well be simpler to return JSON and then use client side scripts to interpret. For example when user=2, reporting "You" instead of "Mike" might be a nice personalization touch. You could do this server side, but now the script needs to take that into account, so the same query needs to return different data based on context (again not impossible). You can keep your server code more generic by using client side scripts to perform this.
Local Presenting - JSON
Perhaps a single command collects the data, but there's multiple parts of the page that should be updated with what's returned. With an HTML approach, you either need separate queries, or some sort of delimiter in your return (with escapes!), and a local processing script to decide what goes where... with a JSON approach, the local processing script can update the locations from the same single source as it's retrieved.
You could approach the question both from the aspect of server burden and in terms of client performance.
If your server is having to dynamically generate the HTML output for every user, it will endure a somewhat higher burden than if you delegated the content-generation to client-side JavaScript. Clients have abundant computing power at their disposal, so feel free to have them collectively shoulder the burden rather than having your server do all the work (which could easily add up, depending on how busy your server is).
Likewise, generating the HTML markup on the server results in a significantly larger page download for the client. The markup for a hundred check-boxes could easily add kilobytes to the size of the page, while the data itself--which is all you would send using the JSON approach--is much smaller. Of course, larger page downloads mean longer download times for the client. We as web developers often forget that there are still quite a few people who still have dial-up internet connections.
For these reasons, I would personally opt for sending the data via JSON and doing DOM-modification via JavaScript.
Cheers,
Josh
The answer is: it depends. If you are going to be doing DOM manipulation on the new data, then you pretty much have to append the elements using jQuery. If there is no such manipulation needed, then you can just print it out with php and add the blob.
I think that the latter is much easier and simpler, so if you don't need to do DOM manipulation on the elements, you can just add the html blob from php.