More Nodejs Design

Key Points

  1. services design concepts with Nodejs


References

Reference_description_with_linked_URLs_____________________________Notes_____________________________________________________________
















Messaging and Notifications
https://github.com/dmx-io/dmx-auctions/blob/development-one/services/auctionEventsPublisher.jspublish auction events to message bus



















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

sample code block
 



Recommended Next Steps