2017-04-21 02:32:53 +00:00
|
|
|
// Subscribe to all new events on the $all stream. Filter out any which aren"t about "user" aggregates.
|
2017-04-21 02:40:09 +00:00
|
|
|
|
2017-04-27 01:10:27 +00:00
|
|
|
const client = require('../src/client')
|
2017-05-17 19:46:27 +00:00
|
|
|
// const client = require("node-eventstore-client")
|
2017-04-21 02:32:53 +00:00
|
|
|
|
|
|
|
const resolveLinkTos = false
|
|
|
|
|
|
|
|
const belongsToAUserAggregate = event =>
|
|
|
|
event.originalEvent.eventStreamId.startsWith("user-")
|
|
|
|
|
|
|
|
const eventAppeared = (subscription, event) => {
|
|
|
|
if (belongsToAUserAggregate(event)) {
|
|
|
|
const aggregateId = event.originalEvent.eventStreamId
|
|
|
|
const eventId = event.originalEvent.eventId
|
|
|
|
const eventType = event.originalEvent.eventType
|
|
|
|
console.log(aggregateId, eventType, eventId)
|
|
|
|
console.log(event.originalEvent.data.toString())
|
|
|
|
}
|
2016-10-31 16:25:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 02:32:53 +00:00
|
|
|
const subscriptionDropped = (subscription, reason, error) =>
|
|
|
|
console.log(error ? error : "Subscription dropped.")
|
|
|
|
|
|
|
|
const credentials = new client.UserCredentials("admin", "changeit")
|
|
|
|
|
|
|
|
const settings = {}
|
2017-04-27 01:10:27 +00:00
|
|
|
const endpoint = "tcp://localhost:1113"
|
2017-04-21 02:32:53 +00:00
|
|
|
const connection = client.createConnection(settings, endpoint)
|
|
|
|
|
|
|
|
connection.connect().catch(err => console.log(err))
|
|
|
|
|
2017-05-15 19:55:14 +00:00
|
|
|
connection.on('heartbeatInfo', heartbeatInfo => {
|
|
|
|
console.log('Connected to endpoint', heartbeatInfo.remoteEndPoint)
|
|
|
|
console.log('Heartbeat latency', heartbeatInfo.responseReceivedAt - heartbeatInfo.requestSentAt)
|
|
|
|
})
|
|
|
|
|
2017-04-21 02:32:53 +00:00
|
|
|
connection.once("connected", tcpEndPoint => {
|
|
|
|
console.log(`Connected to eventstore at ${tcpEndPoint.host}:${tcpEndPoint.port}`)
|
|
|
|
connection.subscribeToAll(
|
|
|
|
resolveLinkTos,
|
|
|
|
eventAppeared,
|
|
|
|
subscriptionDropped,
|
|
|
|
credentials
|
|
|
|
).then(subscription => {
|
|
|
|
console.log(`subscription.isSubscribedToAll: ${subscription.isSubscribedToAll}`),
|
|
|
|
console.log("(To generate a test event, try running 'node store-event.js' in a separate console.)")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
connection.on("error", error =>
|
|
|
|
console.log(`Error occurred on connection: ${error}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
connection.on("closed", reason =>
|
|
|
|
console.log(`Connection closed, reason: ${reason}`)
|
|
|
|
)
|