<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Umakanthan D</title>
    <description>The latest articles on DEV Community by Umakanthan D (@umakanthdiwakar).</description>
    <link>https://dev.to/umakanthdiwakar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94035%2Fe1897409-2ac0-4fa8-a62f-18d36e597009.jpeg</url>
      <title>DEV Community: Umakanthan D</title>
      <link>https://dev.to/umakanthdiwakar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/umakanthdiwakar"/>
    <language>en</language>
    <item>
      <title>0mq - The little bundle of messaging joy</title>
      <dc:creator>Umakanthan D</dc:creator>
      <pubDate>Sun, 02 Jan 2022 06:27:39 +0000</pubDate>
      <link>https://dev.to/umakanthdiwakar/0mq-the-little-bundle-of-messaging-joy-9l9</link>
      <guid>https://dev.to/umakanthdiwakar/0mq-the-little-bundle-of-messaging-joy-9l9</guid>
      <description>&lt;p&gt;0mq (&lt;a href="https://github.com/zeromq/zeromq.js"&gt;https://github.com/zeromq/zeromq.js&lt;/a&gt;) - "A thing of beauty is a joy forever". Or maybe one could go like "Small is Beautiful".&lt;/p&gt;

&lt;p&gt;Nothing more joyful than re-looking at something you thought was perhaps too small and finding that it just rocks!!! A perfect way to start off the year!&lt;/p&gt;

&lt;p&gt;I was looking for micro-designs where I could send out millions of messages and have a set of consumers process them. No message should ever get lost. Plus not having to rely on anything where &lt;em&gt;zookeeper&lt;/em&gt; too comes into the picture :)&lt;/p&gt;

&lt;p&gt;There were a few criteria that I had in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the consumer is not available the producer should wait.&lt;/li&gt;
&lt;li&gt;If the consumer takes more time to process the messages than the time it takes for the producer to produce them, then the producer should queue and wait, sending them in a way that the consumer is able to process them.&lt;/li&gt;
&lt;li&gt;The producer should be able to exit after producing the messages and the consumer should be able to process them.&lt;/li&gt;
&lt;li&gt;Either the producer or consumer can re-start without losing any messages.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;0mq was able to achieve all of the above. Here is a quick and dirty producer and consumer code for you to check out. It is in JavaScript and you will need 'nodejs' to execute it.&lt;/p&gt;

&lt;p&gt;The code here is for the push/pull model. 0mq also supports other models such as pub/sub and request/reply models.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;producer.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Push

  await sock.bind("tcp://127.0.0.1:3000")
  console.log("Producer bound to port 3000")
  let ctr = 0
  let limit = 1000 * 1000
  while (true) {
    await sock.send("do task."+ctr)
    ctr++
    if (ctr % 10000 === 1) console.log(ctr)
    if (ctr &amp;gt; limit) break
  }
  await sock.send("end")
  ctr++
  console.log('sent',ctr)
}

run()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;consumer.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Pull

  sock.connect("tcp://127.0.0.1:3000")
  console.log("Worker connected to port 3000")
  let ctr = 0
  for await (const [msg] of sock) {
    ctr++
    const msg1 = msg.toString()

    if (ctr % 1000 === 1) {
      console.log("%d - message: %s", ctr, msg1)
      await new Promise(resolve =&amp;gt; setTimeout(resolve, 100))
    }
    if (msg1 === 'end') break
  }
  console.log('processed = ', ctr)
}

run()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As for installation, you don't need any server. 0mq is basically a wrapper over the OS socket libraries. For &lt;em&gt;nodejs&lt;/em&gt; all that you need is this one command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install zeromq@6.0.0-beta.6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hats off to the 0mq folks and the brilliant thought and engineering behind this small bundle of messaging joy!&lt;/p&gt;

&lt;p&gt;At 1 million small messages, the test may be simplistic, but the initial results show a lot of promise.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript promises and async functions</title>
      <dc:creator>Umakanthan D</dc:creator>
      <pubDate>Sat, 04 Sep 2021 05:30:56 +0000</pubDate>
      <link>https://dev.to/umakanthdiwakar/javascript-basics-152c</link>
      <guid>https://dev.to/umakanthdiwakar/javascript-basics-152c</guid>
      <description>&lt;p&gt;When learning async/await, I am sure you wrote that function that returns a Promise and you used async/await to check out how it works. But the reverse is also true. You can write an async function and use it as a promise. This is because, an async function anyway returns a promise.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;code&gt;async function myfunc() { return 'Hi';}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can then use it like this&lt;br&gt;
&lt;code&gt;myfunc().then(() =&amp;gt; console.log('got it'))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Similarly you can just replace new Promise and then calling resolve and reject, with an async function that either returns a value or throws an error and use it just like a Promise, for the simple reason, IT IS A PROMISE. Irrespective of whether you return anything or not, an async function ALWAYS RETURNS A PROMISE.&lt;/p&gt;

&lt;p&gt;The following code should demonstrate just that. Of course, once you have an environment where async/await are supported, there is little point in using the old promise syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function f1(num) {
  if (num &amp;lt; 5) return 'Hello world'
  throw 'Error'
}
f1(3)
 .then((data) =&amp;gt; console.log('Got=', data))
 .catch((e) =&amp;gt; console.log('Error=', e))

f1(6)
 .then((data) =&amp;gt; console.log('Got=', data))
 .catch((e) =&amp;gt; console.log('Error=', e))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
