Client Configuration

See our quickstart for a step-by-step walkthrough to begin streaming alerts now!

Python

The Python client is a very lightweight wrapper around confluent-kafka-python.

Open a terminal and run this command to install with pip:
pip install gcn-kafka
or this command to install with with conda:
conda install -c conda-forge gcn-kafka
Save the Python code below to a file called example.py:
from gcn_kafka import Consumer

# Connect as a consumer.
# Warning: don't share the client secret with others.
consumer = Consumer(client_id='fill me in',
                    client_secret='fill me in')

# List all topics
print(consumer.list_topics().topics)

# Subscribe to topics and receive alerts
consumer.subscribe(['gcn.classic.text.FERMI_GBM_FIN_POS',
                    'gcn.classic.text.LVC_INITIAL'])
while True:
    for message in consumer.consume():
        value = message.value()
        print(value)
Run the code by typing this command in the terminal:
python example.py

Node.js

The Node.js client is a very lightweight wrapper around Kafka.js.

Open a terminal and run this command to install with npm:
npm install gcn-kafka
Sample code:
const { Kafka } = require('gcn-kafka')

// Create a client.
// Warning: don't share the client secret with others.
const kafka = new Kafka({
  client_id: 'fill me in',
  client_secret: 'fill me in',
})

// List topics
const admin = kafka.admin()
const topics = await admin.listTopics()
console.log(topics)

// Subscribe to topics and receive alerts
const consumer = kafka.consumer()
await consumer.subscribe({
  topics: [
    'gcn.classic.text.FERMI_GBM_FIN_POS',
    'gcn.classic.text.LVC_INITIAL',
  ],
})

await consumer.run({
  eachMessage: async (payload) => {
    const value = payload.message.value
    console.log(value?.toString())
  },
})
Run the code by typing this command in the terminal:
node example.js