Library to work with Firebase databases in Corona SDK based apps
It utilize Firebase REST API to retrieve, add, change and delete data. Streaming API supported too.
firebase = require('corona_firebase')
local db = firebase('https://corona.firebaseio.com/')
db:get("users", nil, function(event)
if ( event.isError ) then
print( "Network error!" )
else
print ( event.response )
end
end)
main.lua
file contains useful tests. Run it on Corona Emulator and look
into console output to see what happening. It using https://corona.firebaseio.com/ DB for tests.
Importing library returns a db object. It mimics behaviour of Firebase REST API https://www.firebase.com/docs/rest/api/
firebase = require('corona_firebase')
local db = firebase('https://[YOUR_DB_ID_HERE].firebaseio.com/')
On a background network.request
used from Corona SDK https://docs.coronalabs.com/api/library/network/request.html
db:get("[ENDPOINT]", "[QUERY]", function(event)
if ( event.isError ) then
-- Network error
elseif ( event.phase == "began" ) then
-- Request started, something useful may be in event.response
elseif ( event.phase == "progress" ) then
-- Request in progress
elseif ( event.phase == "ended" ) then
-- Finished, data should be in event.response
end
end)
db:post("[ENDPOINT]", '[JSON_DATA]', "[QUERY]", function(event)
-- ..
end)
db:patch("[ENDPOINT]", '[JSON_DATA]', "[QUERY]", function(event)
-- ..
end)
db:put("[ENDPOINT]", '{"key": "value"}', "[QUERY]", function(event)
-- ..
end)
db:delete("[ENDPOINT]", "[QUERY]", function(event)
-- ..
end)
db:on("[ENDPOINT]", "[QUERY]", function( data )
print("Got data!")
print(data)
end)
EventSource / Server-Sent Events
protocol used here. Client's Accept
header is set to text/event-stream
. Every time ''data'' changed on server your application will receive a
notification. Timeout errors handled as well.
Firebase docs on it: https://www.firebase.com/docs/rest/api/#section-streaming
Every method accepts query
string to pass additional query to Firebase REST API.
It accepts fololwing parameters and values: https://www.firebase.com/docs/rest/api/#section-query-parameters
db:get("users", "?auth=CREDENTIAL", function(event)
-- ..
end)