npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hiot-kafka

v4.1.2

Published

hiot-app middleware to bootstrap kafka

Downloads

196

Readme

hiot-kafka

hiot-app middleware with kafka

handleEnvelope

The hiot-kafka package contains a handleEnvelope helper which is there to handle the administrative tasks of handling a kafka message (i.e. logging, acking/rejecting envelopes, handling exception tracking). To use it you need to pass it:

  • A function which handles the kafka message (a function which returns a promise)
  • The kafka broker
  • The application's logger
  • A function which handles exceptions
broker.consume(
  "things.values",
  handleEnvelope(thingValues, broker, logger, onUncaughtException)
);

Then an example handler function would look like this:

const thingValues = async (message, broker) => {
  if (!validator.thingValues(message)) {
    return { status: "ignored", reason: "invalid message" };
  }

  let thingId = message.value.thingId;
  thing = await thingsAPI.fetchThing(thingId);

  if (!thing) {
    return { status: "ignored", reason: "could not find thing" };
  }

  let oldState = {};
  if (thing && thing.state) {
    oldState = thing.state;
  }

  let newState = {
    state: {
      humidity: message.value.values[0].humidity
        ? message.value.values[0].humidity
        : oldState.humidity,
      temperature: message.value.values[0].temperature
        ? message.value.values[0].temperature
        : oldState.temperature,
      battery: message.value.values[0].battery
        ? message.value.values[0].battery
        : oldState.battery,
      connection: "online",
    },
  };

  await thingsAPI.patch(thingId, message.value.correlationId, newState);

  return { status: "handled", thingId };
};

This will produce standard log messages like these to indicate when handling of a message is started and when it is completed (either with a status of ack or a status of reject)

The logs look like this (prettified for easier reading):

{
  "name": "things-svc",
  "hostname": "things-svc-d8b68cb86-s57hb",
  "pid": 1,
  "level": 30,
  "label": "hiot-kafka-handle-envelope",
  "topic": "things.values",
  "msg": "Received message for topic: things.values",
  "time": "2021-04-01T19:48:44.491Z",
  "v": 0
}
{
  "name": "things-svc",
  "hostname": "things-svc-d8b68cb86-s57hb",
  "pid": 1,
  "level": 30,
  "label": "hiot-kafka-handle-envelope",
  "topic": "things.values",
  "status": "ack",
  "metadata": {
    "status": "handled",
    "thingId": "9af5fbc0708711ebb39301f66685f25d"
  },
  "msg": "Received success for things.values",
  "time": "2021-04-01T19:48:44.521Z",
  "v": 0
}

Since the handling function returns a promise, you need to either reject the promise (for the async function above an error is raised) or resolve it (the return statements). When resolving the promise you can optionally return an object which will be put into the metadata key of the logs. This can be used to log a status key (i.e. ignored or handled) and other details which are particularly important to debugging the message (i.e. a thingId or a userId)

A note on ignoring vs. failing

If a service gets a message which it isn't programmed to handled, it is recommended that the message be acknowledged rather that rejected. Kafka is currently configured to simply drop messages when they are rejected, if we ever wanted to implement retries in kafka we should simply handle/acknowledge messages that we want to ignore. Otherwise, if we do implement retries then the message will be retried continually for no reason.

Prettier & ESlint

we use prettier to format our code. Our recommendation is to configure your editor to autoformat during save. If you open up the VS Code User's settings/preferences as UI, search for "Format On Save" and make sure to activate it. Afterward, the file should format automatically once you save it. Now you don’t need to worry about your code formatting anymore, because Prettier takes care of it.

#manual checking and apply
npx prettier --check .
npx prettier --write .

#validate passes eslint
yarn run eslint .
yarn run eslint . --fix
# or
npm run lint
npm run lint-fix