1.0.38 • Published 5 years ago

eventstore-stream-subscriber v1.0.38

Weekly downloads
40
License
ISC
Repository
-
Last release
5 years ago

eventstore-stream-subscriber

Subscribe to an Eventstore stream with the possibility to add separate event handlers for specific event types.

Subscribe to realtime events only

var subscriber = require("eventstore-stream-subscriber")();

// register eventhandlers for eventtypes of a stream
subscriber.registerHandler("myEventType", (subscription, evt) => {
    let e = evt.originalEvent.data.toString();
    console.log("Event received");
    console.log(e);
});

// configure the subscriber
subscriber.configure({
    resolveLinkTos : false,
    logHeartbeats : false
});

// connect and start consuming events
subscriber.createConnection({}).then(() =>
    subscriber.subscribeToStream("myStream")
        .catch((reason) => console.log(reason))
)
.catch((reason) => console.log(reason));

Catch up from beginning of stream and continue to consume realtime events

subscriber.createConnection({}).then(() =>
    subscriber.catchupAndSubscribeToStream("myStream")
        .catch((reason) => console.log(reason))
)
.catch((reason) => console.log(reason));

Read the entire stream from start to end

        // register a global handler (will receive all)
        subscriber.registerGlobalHandler((subscription, evt, eventType) => {
            console.log(`${eventType} event arrived!`);
            console.log(JSON.stringify(evt));
        });

        subscriber.configure({ resolveLinkTos: true });

        const streamName = "Order-123";
        await subscriber.readWholeStreamFromStart(streamName, {}, "c3")
            .then((rs) => console.log("ok"))
            .catch(reason => console.log("reason: " + reason));
            
    });

Configuration

The default configuration:

var configuration = {
    resolveLinkTos : true,
    logHeartbeats : false,
    userName : "admin",
    password : "changeit",
    eventstoreHost : "127.0.0.1",
    eventstorePort : 1113
}

Override these settings in createConnection:

subscriber.createConnection({eventstoreHost : "eventstore.local", eventstorePort : 2113}).then(() =>
    subscriber.catchupAndSubscribeToStream("myStream")
        .catch((reason) => console.log(reason))
)
.catch((reason) => console.log(reason));

Or using .env (make sure to pass these vars to your application)

# eventstore.production.env
EVENTSTORE_STREAM_SUB_HOST=eventstoreprod.somewhere.com
EVENTSTORE_STREAM_SUB_PORT=3113

Since .env overrides the configuration object, both of these will have the same effect when .env variables are present:

//With local config
subscriber.createConnection({eventstoreHost : "eventstore.local", eventstorePort : 2113}).then(() =>
    subscriber.catchupAndSubscribeToStream("myStream")
        .catch((reason) => console.log(reason))
)
.catch((reason) => console.log(reason));

/*
    Resolves to tcp://eventstoreprod.somewhere.com:3113
*/

//without local config
subscriber.createConnection({}).then(() =>
    subscriber.catchupAndSubscribeToStream("myStream")
        .catch((reason) => console.log(reason))
)
.catch((reason) => console.log(reason));

/*
    Resolves to tcp://eventstoreprod.somewhere.com:3113
*/

Logging

The following loglevels can be set in the environment variable EVENTSTORE_STREAM_SUB_LOGLEVEL:

info warning error fatal

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago