-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.js
executable file
·64 lines (45 loc) · 1.45 KB
/
client.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
'use strict';
/**
* Example nodejs EventSource KafkaSSE client.
*
* Usage:
* ./client.js topic1,topic2 [timestamp] [port]
* (port defauts to 6927)
*/
const EventSource = require('eventsource');
const topics = process.argv[2].split(',');
const timestamp = process.argv[3] || undefined;
const port = process.argv[4] || 6927;
let url = `http://localhost:${port}/${topics}`;
if (timestamp) {
url += `?timestamp=${timestamp}`;
}
console.log(`Connecting to Kafka SSE server at ${url}`);
// If used, consume from 'hi' starting at offset.
const simpleAssignments = [
{topic: 'hi', partition: 0, offset: 22060}
];
// Or consume from topic starting 1 hour ago.
const timestampAssignments = topics.map((t) => {
return {topic: t, partition: 0, timestamp: Date.now() - (3600*1000)};
});
let options = {
headers: {}
};
// if not provided timestamp on CLI, use test timestampAssignments.
if (!timestamp) {
options.headers['Last-Event-ID'] = JSON.stringify(timestampAssignments);
}
// options.headers['Last-Event-ID'] = JSON.stringify(simpleAssignments);
let eventSource = new EventSource(url, options);
eventSource.onopen = function(event) {
console.log('--- Opened SSE connection.');
};
eventSource.onerror = function(event) {
console.log('--- Got SSE error', event);
};
eventSource.onmessage = function(event) {
// event.data will be a JSON string containing the message event.
console.log(JSON.parse(event.data));
};