Warning
Starting September 11, 2024, Upstash Kafka has been transitioned into deprecation period, which will last for 6 months. It will eventually be discontinued on March 11, 2025. The demo will still work but not without some changes to the way the events are sent to the Kafka cluster.
This demo utilizes the good old Snake game to demonstrate how to stream events from a game to a Kafka cluster and then run real-time analytics on the events using Materialize.ß
The demo consists of the following components:
- Materialize Cloud
- JavaScript implementation of the Snake game
- Node.js backend API that accepts game events and streams them to Kafka
- Upstash Serverless Kafka cluster
- Deno/Node service to display the highscore list in real-time
You can find a live demo of this project here:
The leaderboard is updated in real-time and can be found here:
psql "postgres://YOUR_USERNAME_HERE@YOUR_MATERIALIZE_CLOUD_INSTANCE.aws.materialize.cloud:6875/materialize"
CREATE CLUSTER game REPLICAS (r1 (size='xsmall') );
SET cluster='game';
Once you have your Materialize cluster up and running, you can create your secrets.
First head to your Upstash account and copy the username and password for your Serverless Kafka cluster.
After that, execute the following statements:
CREATE SECRET sasl_username AS 'pass-here';
CREATE SECRET sasl_password AS 'user-here';
CREATE CONNECTION kafka
FOR KAFKA
BROKER 'YOUR_UPSTASH_KAFKA_CLUSTER_URL_HERE',
SASL MECHANISMS = 'SCRAM-SHA-256',
SASL USERNAME = SECRET sasl_username,
SASL PASSWORD = SECRET sasl_password,
SSL CERTIFICATE AUTHORITY = '';
Using the Upstash Kafka details, create a Kafka source:
CREATE MATERIALIZED SOURCE game
FROM KAFKA CONNECTION kafka
TOPIC 'game-score'
FORMAT JSON;
CREATE OR REPLACE VIEW scoure_board AS
SELECT
*
FROM (
SELECT
(data->>'username')::string AS username,
(data->>'score')::int AS score,
(data->>'id')::string AS ud
FROM (
SELECT CAST(data AS jsonb) AS data
FROM (
SELECT convert_from(data, 'utf8') AS data
FROM game
)
)
);
Query the score board:
select * from scoure_board where score is not null order by score desc limit 100;
Alternatively, you can run the materialize.sql
SQL script to create the above resources in one go.
Note that you need to first edit the
materialize.sql
script to replace the Upstash Kafka cluster URL, username and password to match your environment.
Once you've updated the details in the materialize.sql
script, run the script:
psql "postgres://YOUR_USERNAME_HERE@YOUR_MATERIALIZE_CLOUD_INSTANCE.aws.materialize.cloud:6875/materialize" -f materialize.sql
If you have any questions or comments, please join the Materialize Slack Community!