2016-11-01 14:26:27 +00:00
|
|
|
// Subscribe to all new events on the $all stream. Filter out any which aren't about "user" aggregates.
|
|
|
|
|
2016-10-31 16:25:27 +00:00
|
|
|
var esClient = require('../src/client'); // When running in 'eventstore-node/samples' folder.
|
|
|
|
// var esClient = require('eventstore-node'); // Otherwise
|
|
|
|
|
|
|
|
const credentialsForAllEventsStream = new esClient.UserCredentials("admin", "changeit");
|
|
|
|
const resolveLinkTos = false;
|
|
|
|
|
|
|
|
var esConnection = esClient.createConnection({}, {"hostname": "localhost", "port": 1113});
|
|
|
|
esConnection.connect();
|
|
|
|
esConnection.once('connected', function (tcpEndPoint) {
|
|
|
|
console.log('Connected to eventstore at ' + tcpEndPoint.hostname + ":" + tcpEndPoint.port);
|
2016-11-01 14:26:27 +00:00
|
|
|
esConnection.subscribeToAll(resolveLinkTos, eventAppeared, subscriptionDropped, credentialsForAllEventsStream)
|
|
|
|
.then(function(subscription) {
|
|
|
|
console.log("subscription.isSubscribedToAll: " + subscription.isSubscribedToAll);
|
|
|
|
});
|
2016-10-31 16:25:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function belongsToAUserAggregate(event) {
|
|
|
|
return event.originalEvent.eventStreamId.startsWith("user-")
|
|
|
|
}
|
|
|
|
|
|
|
|
function eventAppeared(subscription, event) {
|
|
|
|
// Ignore all events which aren't about users:
|
|
|
|
if(belongsToAUserAggregate(event)) {
|
|
|
|
var aggregateId = event.originalEvent.eventStreamId;
|
|
|
|
var eventId = event.originalEvent.eventId;
|
|
|
|
var eventType = event.originalEvent.eventType;
|
2016-11-01 14:26:27 +00:00
|
|
|
console.log(aggregateId, eventType, eventId);
|
2016-10-31 16:25:27 +00:00
|
|
|
console.log(event.originalEvent.data.toString() + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function subscriptionDropped(subscription, reason, error) {
|
2016-11-01 14:26:27 +00:00
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2016-10-31 16:25:27 +00:00
|
|
|
console.log('Subscription dropped.');
|
|
|
|
}
|
|
|
|
|
|
|
|
esConnection.on('error', function (err) {
|
|
|
|
console.log('Error occurred on connection:', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
esConnection.on('closed', function (reason) {
|
|
|
|
console.log('Connection closed, reason:', reason);
|
|
|
|
});
|