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

48 lines
1.5 KiB
JavaScript
Raw Normal View History

// Subscribe to all events on the $all stream. Catch up from the beginning, then listen for any new events as they occur.
2017-04-21 02:32:53 +00:00
// This could be used for subscribers which populate read models.
const client = require("eventstore-node")
const eventAppeared => (stream, event) =>
console.log(
event.originalEvent.eventStreamId,
event.originalEvent.eventId,
event.originalEvent.eventType
)
const liveProcessingStarted = () => {
console.log("Caught up with previously stored events. Listening for new events.")
console.log("(To generate a test event, try running 'node store-event.js' in a separate console.)")
}
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 => {
const subscription = connection.subscribeToAllFrom(
null,
true,
eventAppeared,
liveProcessingStarted,
subscriptionDropped,
credentials
)
console.log(`Connected to eventstore at ${tcpEndPoint.host}:${tcpEndPoint.port}`)
console.log(`subscription.isSubscribedToAll: ${subscription.isSubscribedToAll}`)
})
connection.on("error", err =>
console.log(`Error occurred on connection: ${err}`)
)
connection.on("closed", reason =>
console.log(`Connection closed, reason: ${reason}`)
)