General question about networks

Hi everyone, I’ve been using Corona mostly for gaming so far, but now I want to build a business app. I know exactly how to code everything except the networking part.

Basically, I want the app to read news that have been posted by the business owner real-time. So say in the app there will be a tab for a news section, where the owner can post newsletters and the app users can read them by refreshing the page, and say another tab, where the app users can write a post.

I have never done any sort of networking before so I am completely clueless as to how to get this done. I saw something in the corona documents about http asynchronous, but didn’t really understand it. Again, I’m not sure I understand the concept correctly, but I was thinking of using a website that holds all the data as the server. So basically the newsletters are fetched from the website and placed temporarily into the app, and when the app user writes a post, it gets sent and written in the website.

I hope I provided enough information. [import]uid: 83395 topic_id: 23166 reply_id: 323166[/import]

-- Load the relevant LuaSocket modules  
local http = require("socket.http")  
local ltn12 = require("ltn12")  
   
local function downloadNews()  
 -- Create local file for saving data  
 local path = system.pathForFile( "news.txt", system.DocumentsDirectory )  
 newsFile = io.open( path, "w+b" )   
  
 -- Request remote file and save data to local file  
 http.request{  
 url = "http://yoursite.com/news",   
 sink = ltn12.sink.file(newsFile),  
 }  
end  

On your web server, have the news file written in JSON. In your app, you can check to see if you have an internet connection. If you do, call the downloadNews() function, parse the JSON and display it. If there’s no connection, check to see if the app has a news file saved from before, and display news from that. You might want to write “could not update news” at the top. [import]uid: 127106 topic_id: 23166 reply_id: 92646[/import]

Okay, lets try to simplify this as much as possible without writing a whole multi-issue blog post on the topic.

First “Asynchronous” simply means in this case, the network activity happens in the background and your app will be notified when the download is done an opposed to your app stopping to wait on the download to finish (synchronous)

Basically you fire off the request with a function to do something when the download is finished (a “CallBack” function, i.e. the network function “calls back” to your programming saying I’m done. You then have a function that will read the file in and display it however you want.

The method in the post above is a synchronous call or a blocking call in that your app will stop and wait on the download. This isn’t bad for small files, but can hurt the user experience for larger files. I should also point out that if you’re planning on putting this in iTunes for iOS people to download, you probably should change the “system.DocumentsDirectory” to “system.CachesDirectory” since Apple doesn’t want reproducible files from the network in system.DocumentDirectory.

Corona SDK offers two asynchronous tools to download content:

network.download()
http://developer.anscamobile.com/reference/index/networkdownload

and

network.request()
http://developer.anscamobile.com/reference/index/networkrequest

network.download() and network.request() both contact a remote web server and download the resulting content. If you are fetching a document that you need to save to your storage or a big file, you want to use network.download(). network.request() is best for getting small bits of data that you don’t need to store. It sounds like your app could use either with the size of the data being the deciding factor.

To use either of them you need a URL on your webserver that’s going to deliver the data. This could be a text file, XML, JSON, or any other format that your server can deliver.

If you use network.request, the results will be in the event table passed to the function in the response variable (event.response) and then you can deal with the data as necessary.

If you use network.download, the results will be saved to a file, then you have to open the file and read it in.

Regardless you will need to parse the data into something you can use. JSON is a very easy way code wise to manage since it gives you a table to work with. JSON can be scary when you have to try and learn the format, but if you have something that can produce the JSON for you, such as a web-form with PHP where your customer can type in the data and hit save, then in the PHP script, those variables are put into a PHP array, then run PHP’s json_encode() function and deliver the string it outputs as the result of your network.request, then you can do a mytable = json.decode(event.response) to get the data automatically into Corona so you can use it. [import]uid: 19626 topic_id: 23166 reply_id: 92764[/import]

Thank you both so much for the replies!! I understand the theory of it, and have an idea on how to implement it. So basically I need a file that holds the data of the newsletters, and this file should be available on a download link (e.g. www.websitename.com/filename). I know that JSON would be easier to manipulate within the code, but say I want to use a text file instead. I assume this would make it easier for the business owner to add more data and news. Would it be possible to implement this?

Also, is it possible for the app user to send data from the app and store it in another file on the web server? [import]uid: 83395 topic_id: 23166 reply_id: 93022[/import]

Yes, using network.request() you can do both HTTP GET and PUT requests. Depending on the amount of data you need to pass. GET sends data as part of the request URL, i.e.

http://yourserver.com/somescript.php?data=my+url+encoded+data

In your app, you would do something like:

[lua]network.request( “http://yourserver.com/somescript.php?data=my+url+encoded+data”, “GET”, networkListener )[/lua]

Then your PHP script would do something like:

[php]
$data = urldecode($_GET[“data”]);
[/php]

If you need to send enough data that it’s too long for the URL then you would do a PUT request instead.

[lua]local body = “your big chunk of data here”

postData = “data=your+much+bigger+urlencoded+data”

local params = {}
params.body = postData

network.request( “http://yourserver.com/somescript.php”, “POST”, networkListener, params)[/lua]

Then your PHP script would do:

[php]
$data = urldecode($_PUT[“data”]);
[/php] [import]uid: 19626 topic_id: 23166 reply_id: 93029[/import]