We’re excited to announce that the Firebase REST API now supports streaming! This means you can add realtime notifications to your Firebase app on currently unsupported platforms. We’ll outline how to get started with the new streaming protocol, and then we’ll dive into some Ruby and Python examples that make use of streaming.
Streaming from the REST API in 3 Steps
Streaming changes to a location in your Firebase is the equivalent of attaching a single listener to a Firebase reference on any of our client libraries. The new streaming feature makes use of the EventSource / Server-Sent Events protocol, an API for creating an HTTP connection for receiving push notifications from a server.
1. Getting Started
To steam changes to a single location in your Firebase, you’ll need to set the client’s Accept header to “text/event-stream”.
You’ll also need to be sure your app respects HTTP Redirects, specifically HTTP status code 307. The Firebase servers will direct your client to the correct machine for the data that you want to stream via a URL specified in the Location header of an HTTP 307 response.
2. Include the Auth Parameter
If the Firebase location you’re streaming changes to requires permission to read, you must include the auth parameter. The auth parameter is supported by all request types, and authenticates a request to allow access to data protected by your Security and Firebase Rules. The argument can either be your Firebase Secret or an authentication token. Here is an example:
curl
https://SampleChat.firebaseIO-demo.com/users/jack/name.json?auth=CREDENTIAL
3. Handle Events Returned from the Server
As the data at your Firebase location changes, the server will send named events in the following structure:
event: event name
data: JSON encoded data payload
The structure of these messages conforms to the EventSource protocol. The server may send the following five event types: put
, patch
, keep-alive
, cancel
, or auth_revoked
. Here is one example of a set of events returned from the server:
// Set your entire cache to {"a": 1, "b": 2}
event: put
data: {"path": "/", "data": {"a": 1, "b": 2}}
// Put the new data in your cache under the key 'c', so that the complete cache now looks like:
// {"a": 1, "b": 2, "c": {"foo": true, "bar": false}}
event: put
data: {"path": "/c", "data": {"foo": true, "bar": false}}
// For each key in the data, update (or add) the corresponding key in your cache at path /c,
// for a final cache of: {"a": 1, "b": 2, "c": {"foo": 3, "bar": false, "baz": 4}}
event: patch
data: {"path": "/c", "data": {"foo": 3, "baz": 4}}
Check out the Streaming documentation for a full explanation of each event type.
Ruby and Python Examples
We’ve created a simple command line chat client in Python and Ruby to demonstrate the REST API’s new streaming feature. Both chat clients have the same architecture:
-
A thread listening to the Server-Sent Events endpoint from Firebase
-
A thread to POST messages to Firebase
-
A thread to manage the UI
- Curses UI: fancy command line chat
- Basic UI: prints messages to stdout
Simply clone the EventSource-Examples repo on GitHub to get started.
Send Us Your Feedback
We encourage you to try out the new streaming feature and let us know what you think. If you have any questions or feedback, post them in our Firebase Google Group or email me at [greg@firebase.com](mailto:greg@firebase.com?subject=Streaming Feedback). Submit a pull request to our example repo if you have any streaming examples you’d like to share.
We’re excited to see what you create!