"Hello, World! 🎉" in Node.js + TypeScript


Node Rust

In this tutorial, you will learn how to create a topic, build a producer/consumer in Typescript, and sends a “Hello, World! 🎉” message.

Prerequisites 

Before starting on this tutorial, you’ll need to have completed the following

  • Install Node.js (v12.11.0 or above)
  • Have the Fluvio CLI installed and have access to a Fluvio cluster. See our getting started guide.

Create a Topic using the Fluvio CLI 

In Fluvio, we send all of our messages to something called a Topic, which is like a category for related messages. For this tutorial, we’ll create a topic called hello-fluvio using the following command:

$ fluvio topic create hello-fluvio

Check Node.js 

A Fluvio environment for Node requires Node.js v12.11.0 or above.

Install Node.js

Node.js installation varies depending on your operating system.

Operating System Instructions
MacOS Use the official installer from Node.js to install on macOS.
Linux Use the instructions provided by your Linux package manager.
Node.js maintains a list of supported packages.

Writing the Application 

The following sections will setup your project and walk through writing the application files.

Installing Project Dependencies 

Run the following script to setup your project for development:

$ mkdir fluvio-demo && cd fluvio-demo && npm init -y && \
npm install typescript ts-node @types/node -D && \
npm install @fluvio/client -S && \
touch producer.ts consumer.ts

Your working directory should now contain the following files:

$ tree -L 1
.
├── consumer.ts
├── node_modules
├── package-lock.json
├── package.json
└── producer.ts

1 directory, 4 files

Writing the producer.ts File 

Write the following code in your producer.ts file.

This code performs the following actions:
  • Import @fluvio/client and Node.js’ readline modules;
  • Create a new Fluvio Client Instance;
  • Create a connection to a local Fluvio Cluster;
  • Create a new topic producer for hello-fluvio;
  • Listen for input typed into the terminal;
  • Send typed input to the fluvio cluster;
import Fluvio from "@fluvio/client";
import { createInterface } from "readline";

// Create Fluvio Client Instance
const fluvio = new Fluvio();

// Create Readline Instance
const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
});

(async () => {
  // Connect the fluvio cluster;
  await fluvio.connect();

  // Create a topic producer;
  const producer = await fluvio.topicProducer("hello-fluvio");
  const partition = 0;
  console.log("Fluvio Producer created, waiting for input:\n\n>")
  // Relay terminal input to fluvio topic producer;
  rl.on("line", async (input) => {
    await producer.sendRecord(input, partition);
  });
})();

Writing the consumer.ts File 

Write the following code in your consumer.ts file.

This code performs the following actions:
  • Import @fluvio/client module;
  • Create a new Fluvio Client Instance;
  • Create a connection to a local Fluvio Cluster;
  • Create a new topic consumer for hello-fluvio;
  • Listen for events sent by a topic producer;
import Fluvio, { OffsetFrom } from "@fluvio/client";

// Create Fluvio Client Instance
const fluvio = new Fluvio();

(async () => {
  // Connect the fluvio cluster;
  await fluvio.connect();

  const partition = 0;

  //   Create Topic Consumer
  const consumer = await fluvio.partitionConsumer("hello-fluvio", partition);
  console.log("Fluvio Consumer created, listening for events:\n\n")
  await consumer.stream(
    {
      index: 0,
      from: OffsetFrom.Beginning,
    },
    async (msg: string) => {
      console.log(`Received message: ${msg}`);
    }
  );
})();

Running the Demo 

Now that the code is written, we’re ready to run our Hello, World! 🎉 example. Run the following commands in separate terminals.

Running the Producer 

Run the following command in the working directory:

$ npx ts-node ./producer.ts
Fluvio Producer created, waiting for input:

>

Great! Now type Hello, World! 🎉 into your terminal window:

Hello, World! 🎉


Running the Consumer 

Open a new terminal and run the following command:

$ npx ts-node ./consumer.ts
Fluvio Consumer created, listening for events:


Received message: Hello, World! 🎉

Congratulations! 

You’ve now completed the Fluvio “Hello, World! 🎉” tutorial.

Head over to the Fluvio Node documentation to learn more about the library and available options.

Read the @fluvio/client Docs 

Checkout Node API reference guide for additional usage information and documentation.