-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkafka.js
43 lines (38 loc) · 1.16 KB
/
kafka.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Kafka } from 'kafkajs';
const test = process.env.TEST == "true" ? true : false;
// If test mode is true, don't send anything to kafka cluster
const kafka = new Kafka({
brokers: [test ? "" : process.env.KAFKA_URL],
sasl: {
mechanism: test ? "" : process.env.KAFKA_MECHANISM,
username: test ? "" : process.env.KAFKA_USERNAME,
password: test ? "" : process.env.KAFKA_PASSWORD,
},
ssl: true,
})
// setup kafka producer
const producer = kafka.producer()
// Send a message about which word is chosen for drawing
export const sendWordMessage = async (word) => {
await producer.connect()
await producer.send({
topic: 'logs',
messages: [
{ key: "word", value: word },
],
}).then(console.log).catch(e => console.error(e.message));
await producer.disconnect()
return true;
}
// Send number of people in room
export const sendRoomStats = async (count) => {
await producer.connect()
await producer.send({
topic: 'logs',
messages: [
{ key: "room", value: `${count}` },
],
}).then(console.log).catch(e => console.error(e.message));
await producer.disconnect()
return true;
}