Monday, January 30, 2023

Virtual Airport Demo: Connecting DynamoDB to Lightstreamer

We're excited to announce the release of our newest demo. And no, this time it's not a stock-list demo 😉

We switched things up a bit, and the new demo simulates a very simple departures board with a few rows showing real-time flight information to passengers of a hypothetical airport. The data are simulated in the back-end and retrieved from an Amazon DynamoDB data source. In short, we showcase our cool "virtual airport" where you can pretend to take a flight without ever leaving your home!



The live demo is available at https://demos.lightstreamer.com/DynamoDBDemo/

The primary purpose of this demo is to propose an example in which the Data Adapter is powered by an external source and does not internally simulate the data to be sent, like most of our other demos. In this case, it is a NoSQL database service in a cloud environment; to be precise, it is DynamoDB.

DynamoDB is a fully managed NoSQL database service offered by Amazon Web Services (AWS).

Unlike traditional relational database management systems, DynamoDB does not use a fixed schema but supports key-value and document data structures. It offers automatic horizontal scaling; this means that the system can automatically handle an increase in workload and data size without any interruption to the user. DynamoDB also offers low latency for read and write operations, making it suitable for applications that require a high level of performance. In addition, DynamoDB supports geographic replication to ensure high availability of data in case of system failures or connectivity issues.

DynamoDB is used in many applications such as games, social media, e-commerce websites, IoT, and many other business applications. You can access DynamoDB through the AWS web interface, SDKs for various programming languages, or through the programming API.

Overall, DynamoDB is a very flexible and scalable solution for high-performance data management and these features make it a data source perfectly compatible with Lightstreamer's Data Adapter. For example, using the AWS SDK for Java to interact with the DynamoDB and retrieve the data. The adapter can then use this data to update the Lightstreamer server, which will, in turn, push the updates to the connected clients.


The Demo Architecture

The overall architecture of the demo includes the following:
  • A web page using the Lightstreamer Web Client SDK to connect to the Lightstreamer server and subscribe to the flight information items.
  • A Lightstreamer server deployed on an AWS EC2 instance alongside the custom metadata and data adapters. 
  • The adapters use the Java In-Process Adapter SDK and are developed with Kotlin language; in particular the Data Adapter retrieves data from the DynamoDB data source through the AWS SDK for Java.
  • Two DynamoDB tables (DemoCurrentTimeData and DemoDeparturesData).
  • A simulator, also built with Kotlin language, pushing data into DynamoDB tables.


Client Details

The page uses the Web Client SDK API for Lightstreamer to handle communication with the Lightstreamer Server. The page has a simple user interface that is used to display in tabular form the real-time data received from the Lightstreamer Server.

The demo includes the following client-side functionalities:

  • A Subscription containing only 1 item and 1 field, subscribed to in MERGE mode, and updates a DynaGrid displaying the simulated current time (oh yes, in this demo, the time runs a little faster than reality).
  • A Subscription with a single item subscribed to in COMMAND mode, which updates a DynaGrid displaying the current list and status of the next departing flights based on the simulated time.
Updates related to the COMMAND mode item come in the form of add, delete, or update messages and allow respectively adding a new row, removing an existing one, or updating some fields of an existing row of the displayed table. The changes are automatically applied by the Lightstreamer library to the graphical widget, which displays and keeps the grid updated.

Below is the JavaScript code that subscribes to the item.

Adapters Details

The source code of the adapters is basically divided into two packages:

  • server, which implements the Lightstreamer in-process adapters based on the Java In-Process Adapter API . in particular:
    • DemoDepartureProvider.kt implements the DataProvider interface for the simulated flight information;
    • DemoDataProvider.kt implements the DataProvider interface for the current time of the simulation;
    • DemoMetadataProvider.kt implements the Metadata Adapter for the demo; it is a basic extension of the MetadataProviderAdapter that is the default implementation available with the Java In-Process Adapter library.
  • demo, which implements the operations with DynamoDB. In particular, the DynamoData.kt class is responsible for reading information from the DynamoDB table and injecting them into the Lightstreamer server.
In DynamoDB, a stream is a flow of changes to items in a DynamoDB table. When you enable a stream on a table, DynamoDB captures information about every change to data items in the table. This information is written to a stream in the order that the changes were made.
A shard is a unit of data storage within a stream. Each shard contains multiple items and a sequence number that uniquely identifies each item within the shard. The sequence number is assigned by DynamoDB and is unique across all shards in the stream.

The Data Adapter retrieves all available shards for the streams of the two tables involved in the demo, reads the flows, and converts the information about DynamoDB table changes into updates for Lightstreamer COMMAND mode.

Below is a snippet of the Kotlin code of the DynamoData.kt class with the function that reads a shard flow and generates updates to be passed to the Data Adapter.

The code below receives the changes to the tables from the DynamoDB streams, as seen in the code snippet above, and push them into the Lightstreamer server to be dispatched to the web clients in the form of Add, Delete, or Update messages as requested by the COMMAND subscribe mode

The data reception from the streams is triggered by the subscribe function invoked in the Data Adapter when a client subscribes for the first time to the items, specifically in the class DemoDepartureProvider.kt we have this code


The Simulator

The data showed by the demo are randomly generated by a simulator and written into two Dynamo DB tables; the class implementing the simulator is DemoPublisher.kt.
The simulator generates flight data randomly, creating new flights, updating data, and then deleting them once they depart. To be precise, the record in the DemoDeparturesData table contains the following fields: "row", "destination", "departure", "flight", "terminal", "status", and "airline". The "row" field serves as the key for the record in the table and ranges from 1..9.
The table containing the current time of the demo, DemoCurrentTimeData, contains only two fields: "key" and "value" and it is updated as if minutes were seconds.

To Recap


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.