I built an app to rig the chances of me getting a date.

Syed Ahmed
HackerNoon.com
5 min readApr 17, 2019

--

With over a third of people opting to create meaningful relationships online, it’s only fitting that instant gratification-driven dating apps like Tinder and Bumble have flourished. That got me thinking — how hard is it to build a geo-aware mobile dating app from scratch? Turns out, with microservices and serverless design patterns, backed by a realtime network, it’s not so hard.

In this tutorial, we’ll cover two very important parts of building a mobile, geo-aware dating application — geolocation and swiping.

Shoutout to Dan for making this!

Microservices Architecture for a Dating App

Let’s cover the flow of our application and cover a quick overview of what we’ll build. To keep things straightforward, when I say user I’m referring the person who opens the Android app, and when I say partner(s) I’m referring to every other user who opens the application.

We know that we need to find every partner aside from the user, and we also need to know their location. This means that every device needs to share a unique ID and their location. Next, we need each device to be able to check against each other device while also adding themselves to list or updating their current location. Once the user has added themselves to the list of partners, we can choose every other user from the list and check their distance against the current user’s.

That means we can split our whole system up into three parts:

Android Application

The actual Android application that sends it’s own unique ID with location and receives the ID and location of other users.

Save and Filter

This section ingests data from the Android application and returns out the location and unique ID of every user who isn’t the one who called the service.

Calculate Distance

This takes in a user with their location as well as the location of another user and spit back the distance. There is some math involved because we’ll be calculating the distance between two latitude and longitude distances. This service will return the unique user and the distance.

Creating Microservices

To make things simple and efficient, we need to find a provider to run our microservices. To do so, we’ll use PubNub Functions.

You’ll first have to sign up for an account using the embedded form below. After that, head over to the Admin Dashboard and enable the Functions feature.

This will let us build out the Save and Filter feature, as well as the Calculate Distance microservice on PubNub, and give us the realtime, scalable experience we want.

Saving and Filtering Users in Realtime

Our client application will publish the current user’s ID and location to a serverless PubNub Function, which will save the location to a keyset-wide persistent storage called PubNub KV Store.

From there, our first Function will check the current ID against every item in the KV Store and append it to the list of users. Once we have the full list, we’ll publish that message back to channel that’s unique to the device using its ID.

Note: PubNub Functions allows of a maximum of 3 requests per function call.

Calculating Distance in Realtime

We’ll be getting the data in the form of an array. The first two elements of the array are the IDs of the user and the last two elements are the location of the user who initiated the request. The first element is the ID of the initiator, and the second is a possible swipe candidate. Once we finish the calculation, we’ll send the ID of the unique user and the distance they are from the initiator.

The result of this function will look like this:

How to Swipe Through Users on the Android App

To start off, create an empty Android Studio project with Kotlin support checked.

Next, look at the dependencies we’re going to add to our app-level Gradle file to ensure our application runs smoothly.

The first dependency is the PubNub SDK, which will help us publish and subscribe to the logic we just created. Related to the PubNub SDK, we’ll also need our Publish and Subscribe keys. You can get your publish and subscribe keys by going through the quick setup below.

The other dependencies needed are for the visual component of our application — the swiping functionality.

Creating the User Interface

First, we’ll adjust our activity_main.xml to accommodate for our swiping feature that’ll be initialized in our MainActivity.kt file.

Next, we’ll create each profile card’s UI, as well as the overlay on each of them, taking into consideration whether the user is swiping to the left or right.

That’s it for the UI, now let’s cover the backend.

Integrating the Application Logic

For our application to be complete we’ll be creating four separate files. The first file we’re going to need is a class that will act as an object for each profile and will contain the related information.

Next, we’re going to create a file that will have some helper functions to update our collection of profiles.

Now, we can load each profile into the frontend. We’ll do this within a class called the CardStackAdapter.

Stitching Everything Together

We can head over to the MainActivity.kt file to see how everything fits together.

Let’s have a quick look at the onCreate and onStart methods.

We can break down everything that’s happening into three things.

First, we’ll get the location of the device using Fused Location. Next, we’ll subscribe to a channel with the same name as our device ID, since all the possible people we can swipe on are published to that channel. Lastly, in the onStart, we’ll be publishing the date related to the device, just like the ID and Location. The reason we publish in the onStart and not the onCreate is because we won’t be able to get all the information we need to publish until the activity starts.

With that, let’s add all the features and using your pub/sub keys (they’re in your Admin Dashboard), in our MainActivity. In the end, our file will look like this:

Let’s run the app! In either an emulator or on a device, you can see the swiping functionality, as well as the user’s distance from you.

Nice work! Want to explore more features and ideas around mobile dating apps? Check out our realtime dating apps overview, and see how you can power cross-platform, fast, and secure dating apps at global scale with PubNub’s chat APIs and messaging infrastructure.

Originally published at www.pubnub.com.

--

--