# Connect to Kafka

Show the messages of your Kafka topics on the timeline, and select the topics that streamview reads.

Source: https://www.eventcatalog.dev/tools/streamview/docs/how-to/connect-to-kafka

You need the `streamview` program, Node.js, and the address of a broker.

## Show all topics

Run this command with the address of your broker:

```
streamview kafka --brokers localhost:9092
```

The first time, the command installs the Kafka adapter. This takes some seconds.
The browser then opens, and each topic is one lane.

streamview reads all stored messages first, and sorts them by time.
With many messages, this takes some minutes. The page stays empty during this time.

## Connect to a broker that needs TLS or a login

A hosted Kafka service usually needs an encrypted connection (TLS) and a login (SASL).
Your Kafka service gives you the address, the user name, the password, and the login mechanism.

1. Put the password in the environment variable `STREAMVIEW_KAFKA_PASSWORD`. For some services, the password is an API secret.
2. Add `--tls` to the command.
3. Add `--user` with the user name. For some services, the user name is an API key.
4. Add `--mechanism` if the service does not use `plain`. The other mechanisms are `scram-sha-256` and `scram-sha-512`.

```
export STREAMVIEW_KAFKA_PASSWORD='your-password'
streamview kafka --brokers broker.example.com:9093 --tls --user your-user --mechanism scram-sha-512
```

If the broker has a certificate from a private certificate authority, add `--ca` with the PEM file of that authority.
`--ca` also turns on TLS.

```
streamview kafka --brokers broker.example.com:9093 --ca ca.pem --user your-user
```

If the broker asks for a client certificate, add `--cert` and `--key` with the two PEM files.

```
streamview kafka --brokers broker.example.com:9093 --ca ca.pem --cert client.pem --key client-key.pem
```

If the broker refuses the connection, the command prints the cause in the terminal, and tells you which option to change.

:::note
We tested these options with a Redpanda broker. We did not test them with a hosted Kafka service.
The Kafka adapter does not have the IAM login of Amazon MSK, and it does not have OAuth.
:::

## Show only new messages

Add `--latest`. streamview then does not read the stored messages.

```
streamview kafka --brokers localhost:9092 --latest
```

## Show only some topics

Add `--topics` with a regular expression. streamview reads only the topics that match.

```
streamview kafka --brokers localhost:9092 --topics '^(orders|payments)\.'
```

streamview does not read internal topics, for example `__consumer_offsets`, also when the expression matches them.
To read them, add `--include-internal`.

## Connect the messages of one order

If your messages are CloudEvents, streamview uses their `correlationid` field.

If your messages are plain JSON, streamview looks for a `correlationid` header or a `traceparent` header.
If the messages do not have these headers, use one of these two options:

- Add `--correlation-field` with the name of the field that all messages of one order contain.
- Add `--correlation-key` if the key of each message is the ID of the order.

```
streamview kafka --brokers localhost:9092 --correlation-field orderId
```

```
streamview kafka --brokers localhost:9092 --correlation-key
```

Do not use `--correlation-key` if many different orders have the same key. All messages with one key then become one order.

## Continue from the last position

By default, streamview uses a new consumer group for each start, and reads all stored messages.

To continue from the position of the last start, give the consumer group a name with `--group`.
Use the same SQLite file each time, because the file has the events from before.

```
streamview kafka --brokers localhost:9092 --group streamview-team
```

When a group with this name read before, the command prints a line that tells you.
If the page is empty and you expected old messages, use a different name, or remove `--group`.

## Keep the events in a different file

streamview stores the events in `streamview.db` in the current directory.
To use a different file, add `--db` and a path.

```
streamview kafka --brokers localhost:9092 --db /var/lib/streamview/kafka.db
```

When you start streamview again with the same file, it loads the stored events and ignores messages that it already has.

## Use more than one broker

Put a comma between the addresses:

```
streamview kafka --brokers broker-1:9092,broker-2:9092
```

## Related

- [Command line: `streamview kafka`](https://www.eventcatalog.dev/tools/streamview/docs/reference/command-line#streamview-kafka)
- [How streamview reads without taking messages](https://www.eventcatalog.dev/tools/streamview/docs/explanation/how-streamview-reads)
- [Limit how long streamview keeps events](https://www.eventcatalog.dev/tools/streamview/docs/how-to/limit-kept-events)
