@lazyapps/eventbus-mqemitter-redis 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +13 -0
- package/command-receiver/index.js +68 -0
- package/connect.js +7 -0
- package/package.json +52 -0
- package/readmodels/index.js +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Oliver Sturm
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @lazyapps/eventbus-mqemitter-redis
|
|
2
|
+
|
|
3
|
+
MQEmitter-based event bus with Redis backing. Provides distributed event bus capability using MQEmitter with Redis for persistence and cross-process communication.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @lazyapps/eventbus-mqemitter-redis
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Part of LazyApps
|
|
12
|
+
|
|
13
|
+
This package is part of the [LazyApps](https://github.com/oliversturm/lazyapps-libs) event-sourcing and CQRS framework.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import pRetry from 'p-retry';
|
|
2
|
+
|
|
3
|
+
import { getLogger } from '@lazyapps/logger';
|
|
4
|
+
import { connect } from '../connect.js';
|
|
5
|
+
|
|
6
|
+
// NOTE NOTE OLD COMMENT -- may not need this anymore with
|
|
7
|
+
// different other mq systems.
|
|
8
|
+
//
|
|
9
|
+
// For all kinds of reasons (http://zguide.zeromq.org/page:all),
|
|
10
|
+
// zeromq pub/sub subscribers can be running behind the publisher
|
|
11
|
+
// a bit. This can happen when the subscriber is already running
|
|
12
|
+
// before the publisher, but even if it is started shortly after
|
|
13
|
+
// the publisher. Of course there are ways to resolve this and
|
|
14
|
+
// sync things up "properly", but using zeromq in this sample
|
|
15
|
+
// solution is only a suggestion and the issues and solutions
|
|
16
|
+
// would be very different with other messaging systems, so I'm
|
|
17
|
+
// choosing the simple workaround of a small built-in startup
|
|
18
|
+
// delay for now.
|
|
19
|
+
const waitSubscribers =
|
|
20
|
+
(log, millis) =>
|
|
21
|
+
(...args) =>
|
|
22
|
+
new Promise((resolve) => {
|
|
23
|
+
log.debug('Waiting for subscribers to catch up');
|
|
24
|
+
setTimeout(() => resolve(...args), millis);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const mqEmitterRedis =
|
|
28
|
+
({ host = '127.0.0.1', port = 6379 } = {}) =>
|
|
29
|
+
() => {
|
|
30
|
+
const initLog = getLogger('CP/EB/Redis', 'INIT');
|
|
31
|
+
|
|
32
|
+
return pRetry(() => connect({ host, port }), {
|
|
33
|
+
onFailedAttempt: (error) => {
|
|
34
|
+
initLog.error(
|
|
35
|
+
`Attempt ${error.attemptNumber} failed connecting to Redis on port ${port}: '${error}'. Will retry another ${error.retriesLeft} times.`,
|
|
36
|
+
);
|
|
37
|
+
},
|
|
38
|
+
retries: 10,
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
initLog.error(`Failed to connect to Redis on port ${port}: ${err}`);
|
|
42
|
+
})
|
|
43
|
+
.then(waitSubscribers(initLog, 1000))
|
|
44
|
+
.then((mq) => {
|
|
45
|
+
initLog.info(`Event bus publishing to port ${port}`);
|
|
46
|
+
return mq;
|
|
47
|
+
})
|
|
48
|
+
.then((mq) => ({
|
|
49
|
+
publishEvent: (correlationId) => (event) => {
|
|
50
|
+
const log = getLogger('CP/EB/Redis', correlationId);
|
|
51
|
+
log.debug(`Publishing event timestamp ${event.timestamp}`);
|
|
52
|
+
mq.emit({ topic: 'events', payload: { correlationId, event } });
|
|
53
|
+
return event;
|
|
54
|
+
},
|
|
55
|
+
publishReplayState: (correlationId) => (state) => {
|
|
56
|
+
const log = getLogger('CP/EB/Redis', correlationId);
|
|
57
|
+
log.debug(`Publishing replay state ${state}`);
|
|
58
|
+
mq.emit({
|
|
59
|
+
topic: '__system',
|
|
60
|
+
payload: {
|
|
61
|
+
correlationId,
|
|
62
|
+
event: { type: 'SET_REPLAY_STATE', state },
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return state;
|
|
66
|
+
},
|
|
67
|
+
}));
|
|
68
|
+
};
|
package/connect.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lazyapps/eventbus-mqemitter-redis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MQEmitter-based event bus with Redis backing for LazyApps",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./command-receiver": "./command-receiver/index.js",
|
|
8
|
+
"./command-receiver/index.js": "./command-receiver/index.js",
|
|
9
|
+
"./readmodels": "./readmodels/index.js",
|
|
10
|
+
"./readmodels/index.js": "./readmodels/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"*.js",
|
|
14
|
+
"command-receiver/",
|
|
15
|
+
"readmodels/"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"event-sourcing",
|
|
19
|
+
"cqrs",
|
|
20
|
+
"lazyapps",
|
|
21
|
+
"event-bus",
|
|
22
|
+
"mqemitter",
|
|
23
|
+
"redis"
|
|
24
|
+
],
|
|
25
|
+
"author": "Oliver Sturm",
|
|
26
|
+
"license": "ISC",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/oliversturm/lazyapps-libs.git",
|
|
30
|
+
"directory": "packages/eventbus-mqemitter-redis"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/oliversturm/lazyapps-libs/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/oliversturm/lazyapps-libs/tree/main/packages/eventbus-mqemitter-redis#readme",
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18.20.3 || >=20.18.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"eslint": "^8.46.0",
|
|
41
|
+
"vitest": "^4.0.18"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"mqemitter-redis": "^5.0.0",
|
|
45
|
+
"p-retry": "^6.1.0",
|
|
46
|
+
"@lazyapps/logger": "^0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"type": "module",
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "vitest"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import pRetry from 'p-retry';
|
|
2
|
+
|
|
3
|
+
import { getLogger } from '@lazyapps/logger';
|
|
4
|
+
import { connect } from '../connect.js';
|
|
5
|
+
|
|
6
|
+
export const mqEmitterRedis =
|
|
7
|
+
({ host = '127.0.0.1', port = 6379 } = {}) =>
|
|
8
|
+
(context) => {
|
|
9
|
+
const initLog = getLogger('RM/EB/Redis', 'INIT');
|
|
10
|
+
|
|
11
|
+
let inReplay = false;
|
|
12
|
+
|
|
13
|
+
const handleSysMessage = (msg) => {
|
|
14
|
+
switch (msg.type) {
|
|
15
|
+
case 'SET_REPLAY_STATE':
|
|
16
|
+
inReplay = msg.state;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return pRetry(() => connect({ host, port }), {
|
|
22
|
+
onFailedAttempt: (error) => {
|
|
23
|
+
initLog.error(
|
|
24
|
+
`Attempt ${error.attemptNumber} failed connecting to Redis on port ${port}: '${error}'. Will retry another ${error.retriesLeft} times.`,
|
|
25
|
+
);
|
|
26
|
+
},
|
|
27
|
+
retries: 10,
|
|
28
|
+
})
|
|
29
|
+
.catch((err) => {
|
|
30
|
+
initLog.error(`Failed to connect to Redis on port ${port}: ${err}`);
|
|
31
|
+
})
|
|
32
|
+
.then((mq) => {
|
|
33
|
+
mq.on('events', ({ payload }, cb) => {
|
|
34
|
+
const { correlationId, event } = payload;
|
|
35
|
+
const log = getLogger('RM/EB/Redis', correlationId);
|
|
36
|
+
log.debug(`Received event: ${JSON.stringify(event)}`);
|
|
37
|
+
context.projectionHandler.projectEvent(correlationId)(
|
|
38
|
+
event,
|
|
39
|
+
inReplay,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
cb();
|
|
43
|
+
});
|
|
44
|
+
mq.on('__system', ({ payload }, cb) => {
|
|
45
|
+
const { correlationId, event } = payload;
|
|
46
|
+
const log = getLogger('RM/EB/Redis', correlationId);
|
|
47
|
+
log.debug(`Received '__system' event: ${JSON.stringify(event)}`);
|
|
48
|
+
|
|
49
|
+
handleSysMessage(event);
|
|
50
|
+
cb();
|
|
51
|
+
});
|
|
52
|
+
})
|
|
53
|
+
.then(() => {
|
|
54
|
+
initLog.info(`Event bus connected at ${host}:${port}`);
|
|
55
|
+
});
|
|
56
|
+
};
|