node-eventstore-client/samples/subscribe-all-events.js

57 lines
1.9 KiB
JavaScript
Raw Normal View History

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.
const client = require('../src/client')
// 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())
}
}
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 = {}
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))
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}`)
)