-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathexample.js
139 lines (124 loc) · 4.69 KB
/
example.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var EventStoreClient = require("./index");
// Sample application to demonstrate how to use the Event Store Client
/*************************************************************************************************/
// CONFIGURATION
var config = {
'eventStore': {
'address': "127.0.0.1",
'port': 1113,
'stream': '$stats-127.0.0.1:2113',
'credentials': {
'username': "admin",
'password': "changeit"
}
},
'debug': false
};
/*************************************************************************************************/
// Connect to the Event Store
var options = {
host: config.eventStore.address,
port: config.eventStore.port,
debug: config.debug
};
console.log('Connecting to ' + options.host + ':' + options.port + '...');
var connection = new EventStoreClient.Connection(options);
console.log('Connected');
// Ping it to see that its there
connection.sendPing(function(pkg) {
console.log('Received ' + EventStoreClient.Commands.getCommandName(pkg.command) + ' response!');
});
// Subscribe to receive statistics events
var streamId = config.eventStore.stream;
var credentials = config.eventStore.credentials;
var written = false;
var read = false;
var readMissing = false;
var destinationId = "TestStream";
console.log('Writing events to ' + destinationId + '...');
var newEvent = {
eventId: EventStoreClient.Connection.createGuid(),
eventType: 'TestEvent',
data: {
textProperty: "value",
numericProperty: 42
}
};
var newEvents = [ newEvent ];
connection.writeEvents(destinationId, EventStoreClient.ExpectedVersion.Any, false, newEvents, credentials, function(completed) {
console.log('Events written result: ' + EventStoreClient.OperationResult.getName(completed.result));
written = true;
closeIfDone();
});
var nonExistentStreamId = "NoSuchStream";
console.log('Reading events forward from ' + nonExistentStreamId + '...');
connection.readStreamEventsForward(nonExistentStreamId, 0, 100, true, false, onEventAppeared, credentials, function(completed) {
console.log('Received a completed event: ' + EventStoreClient.ReadStreamResult.getName(completed.result) + ' (error: ' + completed.error + ')');
readMissing = true;
closeIfDone();
});
console.log('Reading events forward from ' + streamId + '...');
connection.readStreamEventsForward(streamId, 0, 100, true, false, onEventAppeared, credentials, function(completed) {
console.log('Received a completed event: ' + EventStoreClient.ReadStreamResult.getName(completed.result) + ' (error: ' + completed.error + ')');
read = true;
closeIfDone();
});
console.log('Subscribing to ' + streamId + "...");
var correlationId = connection.subscribeToStream(streamId, true, function(streamEvent) {
onEventAppeared(streamEvent);
connection.unsubscribeFromStream(correlationId, credentials, function() {
console.log("Unsubscribed");
closeIfDone();
});
}, onSubscriptionConfirmed, onSubscriptionDropped, credentials, onSubscriptionNotHandled);
function onEventAppeared(streamEvent) {
if (streamEvent.streamId != streamId) {
console.log("Unknown event from " + streamEvent.streamId);
return;
}
var cpuPercent = Math.ceil(100 * streamEvent.data["proc-cpu"]);
var receivedBytes = streamEvent.data["proc-tcp-receivedBytesTotal"];
var sentBytes = streamEvent.data["proc-tcp-sentBytesTotal"];
console.log(streamEvent.eventNumber + " " + streamEvent.eventId + " - " +
"ES CPU " + cpuPercent + "%, " +
"TCP Bytes Received " + receivedBytes + ", " +
"TCP Bytes Sent " + sentBytes + " - " +
streamEvent.created
);
}
function closeIfDone() {
if (written && read && readMissing) {
console.log("All done!");
connection.close();
}
}
function onSubscriptionConfirmed(confirmation) {
console.log("Subscription confirmed (last commit " + confirmation.lastCommitPosition + ", last event " + confirmation.lastEventNumber + ")");
}
function onSubscriptionDropped(dropped) {
var reason = dropped.reason;
switch (dropped.reason) {
case 0:
reason = "unsubscribed";
break;
case 1:
reason = "access denied";
break;
}
console.log("Subscription dropped (" + reason + ")");
}
function onSubscriptionNotHandled(notHandled) {
var reason = notHandled.reason;
switch (notHandled.reason) {
case 0:
reason = "not ready - retry subscribing"
break;
case 1:
reason = "too busy - retry subscribing"
break;
case 2:
reason = "not master - reconnect to master node"
break;
}
console.log("Subscription not handled (" + reason + ")");
}