WebSockets provide a two-way, realtime, communication stream between endpoints.

To demonstrate how this can be used in an Android project, let’s build a simple drawing app.

We’ll create a fragment with a canvas on which we can draw images. This will stream the drawing actions in realtime to a desktop website (connected to the same WiFi access point).

Android and Chrome screenshots

The full source code is here, follow the README to get set up.

Server-side Implementation

Our server for this demo is the Android app with the WebSocket dependency being provided by our awesome friend Ktor.

Drawing code

The drawing part of this project is…


Source code of final iteration here

Hash maps! With O(1) average insert and query performance they’re our go-to data structure for storing values with a key.

But how do they work? Let’s build one from the ground up in Kotlin to find out. We’ll start with a very basic implementation and then incrementally improve it.

Basic put(), get() and remove() methods

First let’s create the skeleton class with empty put(), get() and remove() methods with generics K and V to indicate the type of keys and values we want to use.

class BasicHashMap<K,V> { fun put(key: K, value: V){ // TODO put value V in the…


Photo by Johannes Plenio on Unsplash

The complete source code for this project is available on GitHub.

Introduction

You may already be enjoying the power and simplicity of Kotlin for writing Android applications but it turns out it’s also a very capable back-end language.

To demonstrate how straightforward it can be, we will take 15 minutes and build a simple API, a coin flipper.

When /flip is called it will simply return the result of a randomised coin flip:

curl localhost:8080/flip{
"face": "TAILS"
}

We will cache the previous results in an in-memory database, the contents of which can be retrieved by calling /outcomes:

curl localhost:8080/outcomes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store