Bicycle-sharing data with NATS and Redis

Chengzhi Zhao
Sep 6, 2018 · 3 min read

Bicycle-sharing is a fun and affordable way to get around town. You pick up a bike at one of the bike stations, pay for time or miles you ride, then return the bike to any stations. It is interesting to consume the public bike-sharing data, there is a standard for bike-sharing feed specification available, which includes fields num_bikes_available, num_docks_available, latitude, and longitude. I built a small application using NATS and Redis to get selected closest stations by a given geolocation.

Tools used

NATS Server is a simple, high-performance open source messaging system for cloud-native applications, IoT messaging, and microservices architectures. NATS is also a Cloud Native Computing Foundation (CNCF) member project. I used NATS as a pub/sub system to get some experience on this project and to get some feel on its highly performant and resilient messaging capabilities.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. I used Redis for storing bike-sharing data and use GEORADIUS in Redis to get selected stations.

satori.com has a nice API to poll live data from multiple bike share stations around the world. We will use its web socket — “wss://open-data.api.satori.com” to poll the data.

Install and Start NATS Server

NATS Server has a quick start page to follow. Since NATS builds from Golang, you will also need to install Go 1.9+ and set your $GOPATH.

go get github.com/nats-io/gnatsd
gnatsd

Install and Start Redis

Install Redis and start Redis server — redis-server

The Pub/Sub Code

bike-sharing-pub.go

bike-sharing-pub.go
line 22: appKey defines api key for satori.com
line 25: establish NATS connection, nats.DefaultURL is 0.0.0.0:4222
line 51-54: publish each message to nats

To start publishing the application, run the following

go run bikeshare-pub.go your_app_key

bike-sharing-async-sub.go

bike-sharing-async-sub.go
line 31: establish NATS connection
line 35: establish Redis connection
line 41-55: subscribe subject "US-Bike-Sharing-Channel" and extract data and load data to Redis

Start to subscribe by following

go run async-sub.go

Let’s start NATS and run pub/sub for a while, you should see the output on the screen.

GEORADIUS to Get Closest Bike-Sharing Stations

GEORADIUS is a nice feature from Redis and returns the members of a sorted set populated with geospatial information, which are within the borders of the area specified with the central location and the maximum distance from the center (the radius).

For example, given key as country, latitude, and longitude in New York City, with 1 kilometers as radius and 3 closest locations.

GEORADIUS US -74 40.7 1 km COUNT 3 ASC

1) "{\"id\":217,\"stationName\":\"Old Fulton St\",\"availableDocks\":36,\"totalDocks\":39,\"latitude\":40.70277159,\"longitude\":-73.99383605,\"statusValue\":\"In Service\",\"statusKey\":1,\"availableBikes\":1,\"stAddress1\":\"Old Fulton St\",\"stAddress2\":\"\",\"city\":\"\",\"postalCode\":\"\",\"location\":\"\",\"altitude\":\"\",\"testStation\":false,\"lastCommunicationTime\":\"2018-08-30 07:26:08 PM\",\"landMark\":\"\",\"country_code\":\"US\",\"channel\":\"US-Bike-Sharing-Channel\",\"publisher\":\"feeds.citibikenyc.com\"}"
2) "{\"id\":391,\"stationName\":\"Clark St \\u0026 Henry St\",\"availableDocks\":31,\"totalDocks\":31,\"latitude\":40.69760127,\"longitude\":-73.99344559,\"statusValue\":\"In Service\",\"statusKey\":1,\"availableBikes\":0,\"stAddress1\":\"Clark St \\u0026 Henry St\",\"stAddress2\":\"\",\"city\":\"\",\"postalCode\":\"\",\"location\":\"\",\"altitude\":\"\",\"testStation\":false,\"lastCommunicationTime\":\"2018-08-30 07:26:10 PM\",\"landMark\":\"\",\"country_code\":\"US\",\"channel\":\"US-Bike-Sharing-Channel\",\"publisher\":\"feeds.citibikenyc.com\"}"
3) "{\"id\":315,\"stationName\":\"South St \\u0026 Gouverneur Ln\",\"availableDocks\":14,\"totalDocks\":29,\"latitude\":40.70355377,\"longitude\":-74.00670227,\"statusValue\":\"In Service\",\"statusKey\":1,\"availableBikes\":14,\"stAddress1\":\"South St \\u0026 Gouverneur Ln\",\"stAddress2\":\"\",\"city\":\"\",\"postalCode\":\"\",\"location\":\"\",\"altitude\":\"\",\"testStation\":false,\"lastCommunicationTime\":\"2018-07-13 05:08:26 PM\",\"landMark\":\"\",\"country_code\":\"US\",\"channel\":\"US-Bike-Sharing-Channel\",\"publisher\":\"feeds.citibikenyc.com\"}"

More detail can be found at repository: https://github.com/ChengzhiZhao/Bike-Sharing-Location-Status-Pubsub

Chengzhi Zhao

Written by

Data Infrastructure Engineering

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade