Firebase + Parse


26th April 2016

Writing a realtime chat support using Python, Firebase and Parse

Want to write a backend support for a realtime chat application ? Learn how to do it using Python, Firebase and Parse

Today we have a lot of different chatting apps, especially on mobile phones. So, if you’re planning to write a chat application on your own application, this post should contain all you need to start doing it with Python, Firebase and Parse.

What’s the motivation behind this?

Messaging is the most used way to communicate, so, if we don’t need to deal with a hard implementation on our backend, we can spend more time thinking about new and awesome features to gain users (and money).

Please, give an example….

Ok, think about a realtime chat app, i.e. WhatsApp. You can send a text message to a contact, make a phone call, send images and videos, send a message to an entire group.

And what is the use case?

We will implement a backend support for a chat application. We will cover two different cases:

Send messages between two different users (‘user-1’, ‘user-2’, where 1 and 2 are the ID of each user respectively).
Send messages to a group (imagine a group between three users, ‘user-1’, ‘user-2’, ‘user-3’, again, 1, 2 and 3 are the ID of each user)

Please remember that we will just cover the implementation on our backend.

What is Python?

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than it would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale

What is firebase?

Firebase is a realtime database which allow us to store & sync data with our NoSQL cloud database. Data is stored as JSON, synced to all connected clients in realtime, and available when your app goes offline.

Setting up firebase

Sign up on Firebase platform clicking here: Firebase Signup .
Once you’re done, write down your Firebase Database name (i.e. ‘my-firebase-db’) and the secret key (both are placed on the dashboard).
For simplicity, I’ve used Python-Firebase to send data to Firebase.
Now, we need to create a ‘chat room’ for our two users (‘user-1’ and ‘user-2’, so, we can call our room ‘room-users-1-2’). This can be done with Python-Firebase in a really simple way: = Firebase(‘room-users-1-2’, auth_token=)

This statement will create a specific location inside the Firebase Database where we can put our messages ( ‘firebase-database-name/room-users-1-2’ ).

We assume that our users are using an android app, Ios app or websockets, and they are listening on that channel too (using the corresponding Firebase sdk). So, every time we put a message inside that location, firebase will trigger a notification to both users, and they will receive it.

Once the users are listening on the proper channels, we can start sending messages, again, this is really easy →

So, as you can see, it is really easy to create a room and put messages inside it. With those few lines, we already resolve the realtime notification goal.

You might think how users know which channel they should listen on for notifications. Well, here is when Parse comes to help us. We can send a Push notification (just like WhatsApp) with the information of the channel to the corresponding user/s.

What is Parse?

Parse handles everything you need to store data securely and efficiently in the cloud. Also, it allows us to send Push notification without worrying about the target platform (Android, Ios, etc)

Setting up Parse

Sign up on Parse. Follow the steps to configure the push notifications for desired platforms. Take note of APP_ID and REST_KEY.
In this case, users should remain listening on a specific channel all the time. We can create a channel with the user ID, ex: ‘user-1’ (as we did with firebase), so, when users login, they must start listening on that Parse channel (using the corresponding Parse sdk).
So, everytime a user sends a message, we put it on the firebase database (as we did before), and, also, we send a push (mobile devices can handle that push, and, if the app is in background, show it, or otherwise omit it). So, how we can send a push to ‘user-1’ ? Easy as this:

self.connection = httplib.HTTPSConnection('', 443)
keys = {
 ‘X-Parse-Application-Id’: self.parse_app_id,
 ‘X-Parse-REST-API-Key’: self.parse_rest_key,
 ‘Content-Type’: ‘application/json’
self.connection.request('POST', '/1/push', json.dumps({
 'channels': [‘user-1’],
 'data': {
   'alert': 'your push message',
   'title': ’your push title’,
   'firebase_channel': ‘room-users-1-2’
}), keys)

If you need to pass too much data on the push, consider creating a Parse object and just pass the object id:

path = '/1/classes//’ ## ex: path = '/1/classes/my-parse-objects/’

object_created = self.connection.request(
object_id = object_created.get('objectId')

So, change the code:

self.connection.request('POST', '/1/push', json.dumps({
 ‘channels': [‘user-1’],
 'data': {
   'alert': 'your push message',
   'title': ’your push title’,
   ‘object_id’: object_id,
   'firebase_channel': ‘room-users-1-2’
}), keys)

If needed, devices can ask Parse for an object with id = object_id. Parse allows us to send multimedia content too.

Final Words…

That’s all you need. We’ve already implemented our core, using Firebase to allow different devices to stay synchronized, and Parse to send push notifications. So, it’s time to write your own realtime application.
Please take a look at the documentation, both platforms offer a lot of useful features that I’ve not described in this post.