Skip to content

[Work in Progress] πŸš„ Live tracking North American intercity passenger rail β€“Β πŸ‡ΊπŸ‡Έ Amtrak, πŸ‡¨πŸ‡¦ VIA Rail, 🌴 Brightline

Notifications You must be signed in to change notification settings

liddiard/traintracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

415 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš„ TrainTracker

Live tracking North American intercity passenger rail – πŸ‡ΊπŸ‡Έ Amtrak, πŸ‡¨πŸ‡¦ VIA Rail, 🌴 Brightline. Check train schedules with a live-updating timeline, and visualize realtime positions on an interactive map.

Features

  • πŸš† Train search, filter, and sort
  • πŸ“ Live train positions displayed on a map, extrapolated between GPS updates
  • πŸ”” Per-stop push notifications for arrivals and departures
  • πŸŒ— Light/dark mode, configurable units and timezone display
  • πŸ“± Desktop- and mobile-optimized UIs

Getting Started

npm install
npm run dev

Opens at http://localhost:3000.

Environment variables

Copy .env.example to .env and fill in your VAPID keys (required for push notifications):

cp .env.example .env

Generate VAPID keys with:

npx web-push generate-vapid-keys

Commands

Command Description
npm run dev Start the development server
npm run build Build for production
npm start Run the production build
npm run lint Lint and format check
npx tsc --noEmit Type check

Architecture

Data flow

  1. API layer (app/api/trains/) β€” Fetches and decrypts train data from Amtrak, VIA Rail, and Brightline data providers
  2. Train provider (app/providers/train.tsx) β€” Polls the API seconds and distributes data via React context
  3. Track snapping (app/components/Map/calc.ts) β€” Snaps GPS coordinates to the nearest point on track geometries using Turf.js; extrapolates position between updates based on timetable
  4. Map rendering (app/components/Map/) β€” Displays trains on a MapLibre GL map via react-map-gl with custom markers, labels, and route styling

Key directories

app/
β”œβ”€β”€ api/             # Next.js API routes (trains, notifications)
β”œβ”€β”€ components/      # React components (Map, timeline, notifications)
β”‚   └── Map/         # Map system: rendering, track snapping, display logic
β”œβ”€β”€ providers/       # React context providers (train data, settings)
β”œβ”€β”€ lib/             # Shared utilities (Prisma client, notification polling)
└── types.ts         # Shared TypeScript types
public/
└── map_data/        # GeoJSON track geometries
db/
β”œβ”€β”€ schema.prisma    # Database schema
└── migrations/      # Prisma migrations

GTFS data import

On startup, app/lib/gtfs-import.ts downloads and parses GTFS feeds for each agency, then:

  1. Imports stops and trips into the SQLite database (used at runtime for station lookups and track shape resolution)
  2. Generates public/map_data/track.json β€” the GeoJSON LineString features used to render tracks on the map

Results are cached for 24 hours (tracked via GtfsImportMeta) so subsequent restarts skip the download. In production the filesystem is read-only, so the GeoJSON is pre-built during the Docker image build and only the database upsert runs at startup.

Database

Uses SQLite via Prisma. The database file (db/app.db) is created automatically on first run; migrations run on startup. The schema has four models:

Model Purpose
PushSubscription Web push credentials, plus the train, stop, and notification type a user subscribed to
GtfsStop Station records from GTFS feeds (code, name, coordinates, timezone)
GtfsTrip Trip records linking train numbers to GTFS shape IDs
GtfsImportMeta Singleton that records the last successful import time for the 24-hour cache

Push notifications

A server-side background poller (app/lib/notifications.ts) checks train status every 30 seconds and sends web push notifications 5 minutes before a subscribed arrival or departure. A service worker (public/service-worker.js) handles delivery in the browser background.

Deployment

See DEPLOYMENT.md for instructions on deploying to a VPS with Docker and nginx.

About

[Work in Progress] πŸš„ Live tracking North American intercity passenger rail β€“Β πŸ‡ΊπŸ‡Έ Amtrak, πŸ‡¨πŸ‡¦ VIA Rail, 🌴 Brightline

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors