Building a Telemetry for Android/iOS Realtime App

k8scale.io
3 min readMay 25, 2020

Today we will discuss about how to build a telemetry system while building an app. In this we will be taking of how we do it in Coral Server which we have built for real time apps.

Why telemetry is important?

Telemetry is important to capture the events happening on the device with-in your app. These events can then be joined with other data to draw useful conclustions.

An example for one of our clients: Restaurants get order notification on tablet which results into a ringing bell. Once they press the accept button, they have to open the order and confirm it or cancel. Now for our system its very hard to know that whether the bell rang and then he saw the order details. In the above case the telemetry helps us to stich the events together.

Devices connected to Coral Server

How to build a simple Telemetry System?

Lets first look into device side code and we will be taking android as an example. We will be putting java code here. We use a blockingQueue to capture Events from the app in ordered manner. Since we don’t want to publish events as they happen. We can push them to the server async.

You can add an event to the event manager by just calling the addEvent api. In the above code we can capture upto 500 events without blocking.

Below is rough diagram of the different classes and there flow.

Each activity where you want to publish events you simple call Telemetry Manager api with event information.

TelemetryMgr.addEvent(Event.createEvent("AlertDismissed"));

You write a background thread which in the above diagram we are calling as TelemetryUploadWorker to push the events to server. In the below code we are publishing 10 events at a time.

WebSocket webSocket = application.getWebSocket();
int eventCount = 0;
List<Event> events = TelemetryMgr.getEvents(10);
boolean send = false;
if(events !=null && !events.isEmpty()) {
System.out.println("Publishing telemetry data");
TelemetryData telemetryData = new TelemetryData();
telemetryData.setRestaurantCode(application.getRestaurant().getCode());
telemetryData.setEvents(events);
String serializedData = gson.toJson(telemetryData);
String encodedData = BaseEncoding.base64().encode(serializedData.getBytes(Charset.defaultCharset()));

ClientMessage msg = new ClientMessage();
msg.setAuthToken(application.getApiToken());
msg.setCommand("Telemetry");
msg.setData(encodedData);
msg.setSessionId(application.getSessionId());
send = webSocket.send(gson.toJson(msg));
}

What happens on server side?

Since the Coral Server uses websocket we stream these events to the server.

On Server we further batch this events into bigger objects and dump periodically in Google cloud storage.

For the most of brief duration we buffer the batches in memory and once they reach a size of roughly 1MB we flush them to cloud storage.

Let us know if you have questions on this way of capturing Telemetry from an Android app.

A simple Telemetry client code can be found in the repository

Reach out to us if you are interested in using Coral Server for your real time apps.

--

--