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

52 lines
1.7 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')
2017-04-21 02:32:53 +00:00
const client = require("eventstore-node")
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 = { host: "localhost", port: 1113 }
const connection = client.createConnection(settings, endpoint)
connection.connect().catch(err => console.log(err))
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}`)
)