This is a demo application built using Next.js and HarperDB. The application provides real-time weather forecasts for flights, allowing users to check weather conditions for departing and arriving airports and subscribe to updates.
- Real-Time Weather Forecasts: Check and subscribe to weather forecasts for flights using the OpenWeather API.
- Session Management: Each user session is tracked using a unique session ID stored in the browser's local storage.
- MQTT Integration: Real-time updates are delivered using MQTT.
- REST APIs: The application uses HarperDB REST APIs for data fetching and manipulation.
- Node.js
- npm
- HarperDB
-
Clone the repository:
git clone https://github.com/tristanlee85/harperdb-demo-app.git cd harperdb-demo-app
-
Install dependencies:
npm install
-
Set up environment variables:
The application requires an OpenWeather API key, which should be set in the
.env
file and should automatically be copied during thenpm install
step, but if it is not, you can create it by copying.env.example
to.env
andapp/.env
.
To start the development server, run:
npm run dev
This will start HarperDB on http://localhost:9926 and the Next.js application at http://localhost:9926/app.
When HarperDB is started, the AirportCode
table will be populated with airport codes from an external source if it is empty.
To demo the application:
- Open http://localhost:9926/app
- Select departure and arrival airports, and the times of the flight.
- Click Check Flight Weather 🌦️ to fetch the weather forecast closest to the flight time.
- Click Subscribe for Updates to subscribe to weather updates for the flight. This will create a
Subscriber
record and correspondingForecastSubscription
records for the forecast. - Wait for Updates: After subscribing, wait for approximately 15 seconds. The application will simulate a forecast update by modifying the temperature slightly. This is done to demonstrate the real-time update feature using MQTT as the OpenWeather API does not support real-time updates.
- View Real-Time Updates: Any changes to the subscribed forecast will be displayed under the Real-Time Forecast Updates section. This is handled by the
LiveForecast
component, which connects to the MQTT broker and listens for messages on the subscribed topics.
- Frontend: The application is built using Next.js. The main page is located in
app/src/app/page.js
. - Backend: The backend logic is implemented using HarperDB's custom resources in
resources.js
. Key resources include:AirportsByCountry
: Fetches airport data based on the country code.CheckTravelWeather
: Retrieves weather forecasts for specified airports and times.ForecastSubscription
: Handles joining of external forecast data to the forecast subscription record to obtain the latest temperature. It also overrides thesubscribe()
method to intercept messages for adding custom fields.SubscribeToForecast
: Used to create aSubscriber
record and correspondingForecastSubscription
records for the forecast.
- Real-Time Updates: The application uses MQTT for real-time updates. The
LiveForecast
component subscribes to forecast topics and displays updates as they are received.