Key Points
- services design concepts with Nodejs
References
Key Concepts
Event Publishing
https://github.com/dmx-io/dmx-auctions/blob/development-one/services/auctionEventsPublisher.js
Nodejs example
https://github.com/dmx-io/dmx-auctions/blob/development-one/services/auctionEventsPublisher.js
const { getLogger } = require('@dmx/npm-logger');
const { pubMq } = require('../lib/mq/pub');
const { getPublicAuction, getPublicBidEvent } = require('./getPublicEvent');
async function _publishAuctionEvent(event, payload) {
try {
await pubMq.publish({
exchangeName: 'auctions',
routingKey: event,
payload,
});
} catch (error) {
getLogger().error(`Cannot publish ${event} event into a message queue`, error);
}
}
async function publishAuctionEvent(event, auctionDoc, additionalPayload = {}) {
const auction = typeof auctionDoc.toObject === 'function' ? auctionDoc.toObject() : auctionDoc;
const publicEvent = { auction: await getPublicAuction(auction) };
const payload = {
auction,
publicEvent,
...additionalPayload,
};
await _publishAuctionEvent(`auction.${event}`, payload);
}
async function publishAuctionBidEvent(event, auctionDoc, bidDoc) {
const auction = typeof auctionDoc.toObject === 'function' ? auctionDoc.toObject() : auctionDoc;
const bid = typeof bidDoc.toObject === 'function' ? bidDoc.toObject() : bidDoc;
const publicEvent = { auction: await getPublicAuction(auction, bid), bid: await getPublicBidEvent(bid) };
const payload = {
auction,
bid,
publicEvent,
};
await _publishAuctionEvent(`auction.bid.${event}`, payload);
}
module.exports = {
publishAuctionEvent,
publishAuctionBidEvent,
};
Event Listening
Potential Value Opportunities
Potential Challenges
Candidate Solutions
Step-by-step guide for Example
sample code block
Recommended Next Steps
Related articles