GTFS Realtime¶
The GTFS Realtime module implements the General Transit Feed Specification (GTFS) Realtime for Kotlin Multiplatform.
GTFS Realtime is a standardized data feed for real-time transit information such as trip updates, vehicle positions, and service alerts. It uses Protocol Buffers as its wire format.
Features¶
- Protobuf encoding and decoding with kotlinx-serialization
- HTTP client for fetching feeds using Ktor
- Strong type safety with Kotlin data classes and enums
- Kotlin Multiplatform support (JVM, Native, JS, WASM)
Installation¶
Add the dependency to your build.gradle.kts. The client functionality requires Ktor, so also add a
Ktor engine:
dependencies {
implementation("dev.sargunv.mobility-data:gtfs-realtime:0.5.0")
implementation("io.ktor:ktor-client-cio:3.5.2") // or another engine
}
Example¶
GtfsRealtimeClient().use { gtfsRt -> // (1)!
val feed =
gtfsRt
.getFeedMessage( // (2)!
feedUrl = "https://gtfsr.tri-rail.com/download.aspx?file=trip_updates.pb"
)
.getOrThrow()
val tripUpdates = feed.entity.count { it.tripUpdate != null } // (3)!
val vehiclePositions = feed.entity.count { it.vehicle != null }
val alerts = feed.entity.count { it.alert != null }
println("Trip updates: $tripUpdates")
println("Vehicle positions: $vehiclePositions")
println("Alerts: $alerts")
}
- Create a GTFS Realtime client instance. The client implements
AutoCloseableso it can be used with.useto ensure proper cleanup. - Fetch and decode a GTFS Realtime protobuf feed from a URL.
- Inspect the feed entities for trip updates, vehicle positions, and alerts.
API Reference¶
See the API Reference.