Firebase observer using REST Api
santoshkc05 opened this issue · 3 comments
I am building IOS application using firebase REST API. From the doc (Ref: https://firebase.google.com/docs/reference/rest/database/) it says that we can Stream from the REST API.I am not able to listen firebase events.When I run the app Only onOpen callback is called. When i changed the child value on firebase nothing happened on callback methods. Can you suggest me the way to observe the events?
I tried following code:
Auth.auth().currentUser?.getIDTokenForcingRefresh(true, completion: { (token, error) in
let server : String = "https://project-XXXXX.firebaseio.com/.json?auth=\(token!)"
let eventSource: EventSource = EventSource(url: server)
eventSource.onOpen {
// When opened
debugPrint("eventSource open")
}
eventSource.onError { (error) in
// When errors
debugPrint("error = \(error?.localizedDescription)")
}
eventSource.onMessage { (id, event, data) in
debugPrint("data = \(data)")
// Here you get an event without event name!
}
eventSource.addEventListener("child_added") { (id, event, data) in
debugPrint("data = \(data)")
// Here you get an event 'event-name'
}
})
Hi @Alice05, I had the same problem but I was expecting that .onMessage method worked but wasn't
the case.
Reading the official Firebase documentation for "Streaming data", I realized that all the events you can get are the following ones:
put
patch
keep-alive
cancel
auth_revoked
You are adding the listener to 'child_added' event, that is not a valid one.
put worked for me, every single change in that end point is presented now.
self.eventSource?.addEventListener("put") { (id, event, data) in print("UPDATE!!!!") print("ID:\(id)\nEVENT:\(event)\nDATA:\(data)") }
Hope this will help you!
https://firebase.google.com/docs/reference/rest/database/#section-streaming
WOW, thanks a lot.. It really help me.
@santoshkc05
Hi Santosh
How did you disconnect connect to server?
I tried using
eventSource.disconnect()
But not working. Can you please help me?