ES6 Spread/Rest Operator

ES6 has a handy feature called the Spread/Rest operator notated as .... The reason is has two names is because based on the use, it can do two different things.

Spread

For example, we can spread an array of values into a function parameters.

function myFunction(a, b, c) {
   console.log(a, b, c)
}

myFunction(...[1, 2, 3]) // outputs 1, 2, 3

We can aslo spread one array into another to improve array concatenation like so:

const a = [1, 2];
const b = [3, 4]'
const c = [...a, ...b];

console.log(c); // Results in [1, 2, 3, 4]

Rest

You can also use the operator to gather items into an array. A common usage of this is accepting extra arguments into a function

function myFunction(one, ...args) {
    console.log(args);
}

myFunction(1, 2, 3); // prints 1, [2, 3]

For more info, check the MDN docs

Array Slice in Javascript

Something I need to do freqeuntly in Javascript (and most programming languages) is get a sub array from a larger array. Javascript provides the handy Array.slice method for this. Here is a quick snippet on how to use it.

const arrray = [1, 2, 3, 4];
const subarray = arrray.slice(1, 3);

console.log(subarray) // prints 2, 3, 4

Great Resource for Learning Regex in Javascript

Regex is a powerful way to searching in most every programming language. Althought when someone new to regex views one, they can become intimidated quickly. I’ll admit some regex strings are daunting, but with practice, this ambiguity clears (mostly :P).

Here are some create resoruces for learning Regex.

flaviocopes – This site has a great walkthrough, and slowly builds up to some more difficult Regex.

Learn Regex the Easy Way – A great tutorial that is language agnostic, but is a great from beginning to advance.

Learn Regular Expressions for Beginners – Back to the Javascript tutorials with this great one.

https://regex101.com/ – This isn’t a tutorial, but is a great way to test out your Regex

 

Node.js Sleep using Async/Await

Are you also downloading lots of data from an API and need a simple way to throttle? Here is a quick snippet on how to use sleep (a function common in many languages).

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function work() {
  console.log('Start sleeping');
  await sleep(5000);
  console.log('Five seconds later');
}

work();

 

Dockerizing a Node.js App

In this tutorial, I’ll quickly show you how to Dockerize a Nodejs app. I want cover what docker is or why – hopefully that’s why you found this post. Let’s get to it.

Creating the Project

Start by creating a new project

mkdir nodejs-docker
cd nodejs-docker/
npm init -y
touch index.js
touch Dockerfile

The only thing new above should be the Dockerfile. This is where we will declare the commands for docker to run.

And here is the docker file with coments on the steps.

FROM node:8 # Tell Docker to start from a Node:8 image (this is maintained by node)

WORKDIR /home/user/nodejs-docker # Add the absolute path to our project directory

COPY . . # Copy all of the current directory (the one above) to the Docker directory

RUN npm install --production # Install everything we need from production

CMD ["node", "index.js"] # Run the app!

Building and Running

Now that we have the project set up, let’s build and run the app.

Here is the build command. This builds our current directory (find that Dockerfile) and creates and image.

docker build -t nodejs-docker .

We can view images on our machine with this.

docker image ls

Finally, to run the command we do the following command:

docker run -p 8080:80 nodejs-docker

This command runs our docker image and forward the outside port 8080 to port 80 in the docker container. To get this to work, you will need to build something like an Express app to listen to port 80.

And, that’s about it! If you like to explore further, I recommend checking out this blog: https://nodesource.com/blog/containerizing-node-js-applications-with-docker and the official docs: https://docs.docker.com/get-started/

Sending Email in Node.JS with Amazon SES

Amazone Simple Email Service (SES) is a cheap and easy way to send transactional emails to your users. I use this on many projects to do “lifecycle marketing” (activity updates, reminders) and sending items like password reset. In this tutorial, we will set up Amazon SES and send a quick email.

Prereqs

Start by setting up your aws keys.
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html?shortFooter=true

Getting Setup

First let’s create a new directiory and set up a npm project.

mkdir amazon-ses
cd amazon-ses/
npm init -y

 Then, install the aws-sdk library and create a new file for our email program.

npm install aws-sdk --save
touch send-email.js

Sending the Email

We start off by configuring our AWS library and creating a new instance of AWS.SES which allows us to send the emails.

const AWS = require('aws-sdk');

AWS.config.update({ region: 'us-west-2' });

const SES = new AWS.SES({ apiVersion: '2010-12-01' });

Now, to send the email we simply use the sendEmail and pass it a list of params. We also call .promise() so we can have a promise, of course 😀

const fromEmailAddress = 'myadmin@admin.com';
const testEmailAddress = 'test@test.com';

const params = {
  Destination: {
    CcAddresses: [
      testEmailAddress,
    ],
    ToAddresses: [
      testEmailAddress,
    ],
  },
  Message: {
    Body: {
      Html: {
       Charset: 'UTF-8',
       Data: '<div>Html!</div>',
      },
      Text: {
       Charset: 'UTF-8',
       Data: 'test email',
      }
     },
     Subject: {
      Charset: 'UTF-8',
      Data: 'Test email'
     }
    },
  Source: fromEmailAddress,
  ReplyToAddresses: [
    testEmailAddress,
  ],
};

const sendPromise = SES.sendEmail(params).promise();
sendPromise.then((data) =&gt; {
    console.log(data.MessageId);
  }).catch((err) =&gt; {
    console.error(err, err.stack);
  });

The params are rather large, but it will get the job done!

Using Redis as a Queue in Nodejs

When scaling out a website, sometimes you will have cpu intensive tasks or tasks that the user should not wait on when making a API request. For these kind of activities, one option is to create a Queue. You send a message to one server and have a worker (workers) read that request and process.

For example, say the user sends a request to complete a todo (like at Habitica ;)) and you then need to send webhooks that are subscribed to this action. This is a great time to set up a queue and worker.

For more ideas on queue and how to scale, check out this repo.

Using Redis Simple Message Queue

For this tutorial, let’s get started with a repo I found rightly called Redis Simple Message Queue.

You will first need to install Redis on your system, once you have that, let’s build the producer. The producer will send request to Redis.

Create a new directory and a file for the producer.

mkdir redis-simple-queue
cd redis-simple-queue
touch producer.js

Next, let’s init a new npm project and install rsmq

npm init -y
npm install rsmq --save

Creating the Producer

Now, in the producer.js, let’s add the following to connect to Redis. We first import the new library, then connect to Redis assumed on our localhost.

const RedisSMQ = require("rsmq");
const rsmq = new RedisSMQ({
  host: "127.0.0.1",
  port: 6379, ns: "rsmq"
});

Then, we should create a queue that will listen to our webhook requests. Note that you can have multiple queues for all sorts of processes.

rsmq.createQueue({ qname: 'webhook-queue'}, (err, resp) => {
	if (resp === 1) console.log('queue created');
});

Alright! Almost done here. Now that we are connect, let’s send a message.

rsmq.sendMessage({
    qname: 'webhook-queue',
    message: 'Hello World',
  }, (err, resp) => {
  	if (resp) console.log('Message sent. ID:', resp);
  });

This is a good example of using a producer, but for our scenario at the beginning, we would be calling this send message inside of our Express.js route request.

The full code for our producer is here.

const RedisSMQ = require("rsmq");
const rsmq = new RedisSMQ({
  host: "127.0.0.1",
  port: 6379, ns: "rsmq"
});


rsmq.createQueue({ qname: 'webhook-queue'}, (err, resp) => {
	if (resp === 1) console.log('queue created');

  rsmq.sendMessage({
    qname: 'webhook-queue',
    message: 'Hello World',
  }, (err, resp) => {
  	if (resp) console.log('Message sent. ID:', resp);
  });
});

Building a Worker

Okay, now we are ready to create a worker to process. First create a new file.

touch worker.js

Then, let’s add the connection logic again.

const RedisSMQ = require("rsmq");
const rsmq = new RedisSMQ({
  host: "127.0.0.1",
  port: 6379, ns: "rsmq"
});

 
Now, we add a little snippet to recieve the message. And you can see where we will then handle the logic for the request.

rsmq.receiveMessage({
  qname: 'webhook-queue',
}, (err, resp) => {
	if (!resp.id) return;
	console.log('Message received.', resp);
  // Do long logic here
});

We are almost done. Now that we have processed the request, we need to remove the request from the queue so no other worker processes it. The reason we don’t automatically remove the queue when we send it is for retries. So, if one work fails, we can just resend the request.

rsmq.deleteMessage({
      qname: 'webhook-queue',
      id: resp.id,
  }, (err, resp) => {
  	if (resp === 1) console.log('Message deleted.');
  });

And the full code is here.

const RedisSMQ = require("rsmq");
const rsmq = new RedisSMQ({
  host: "127.0.0.1",
  port: 6379, ns: "rsmq"
});

rsmq.receiveMessage({
  qname: 'webhook-queue',
}, (err, resp) => {
	if (!resp || !resp.id) return;
	console.log('Message received.', resp);

  // Do long logic here

  rsmq.deleteMessage({
      qname: 'webhook-queue',
      id: resp.id,
  }, (err, resp) => {
  	if (resp === 1) console.log('Message deleted.');
  });
});

Conclusion

This is a very short introduction to queues, but I’m hoping you can see the power of queue already. There is much more to explore: how to use multiple workers, how to do retries and timeouts and how to orchestrate all of this. We leave this to explore in the future :D.

Creating Your First Slack Bot in Nodejs

If you are looking for a good introduction to the world of AI or are simply wanting to be trendy and cool, Chat Bots are the answer. In this post, I’ll get you started with a quick chat bot in Nodejs.

Getting Permissions

First, we will need to let Slack know about our bot and grab an API key.

  1. Go to this URL: https://my.slack.com/services/new/bot
  2. Enter a username for your bot. We will call this one, testbotScreen Shot 2018-04-14 at 12.05.38 AM.png
  3. Scroll down to Integration Settings and grab the API key. I’ve deleted the one below 😛Screen Shot 2018-04-14 at 12.07.52 AM.png

Creating the Nodejs Bot

Alright, now that we have our creds, let’s create a bot.

First, create a new directory, initialize a project and install the slackbots library.

mkdir my-first-bot
cd my-first-bot
npm init -y
npm install slackbots --save
touch index.js

Alright! So now, let’s initialize the bot using the API key from above. In the ​index.js add the following.

const SlackBot = require('slackbots');
const bot = new SlackBot({
    token: 'xoxb-346962835285-DOgbKAsXuvn5tTYOrlUOyJYB', 
    name: 'My Bot'
});

Sweet, now let’s create an on start message to let everyone know we are connected.

bot.on('start', () => {
    bot.postMessageToChannel('general', 'Hello!');
});

The above will post a hello message in the general channel when the bot has connected. One last thing we shoul do for this tutorial is respond to users. Let’s start by adding the on message event.

bot.on('message'(data) => {
    console.log(data);
});
Next, we will check if the user is messaging our bot and then reply to them if so.
bot.on('message'(data) => {
    if (msg.username === 'My Bot') {
       bot.postMessage(data.channel, 'Yo!);
    }
});
The above checks if the username of the message is to our bot and sends a reply to the user in the same channel.
And that’s all for this tutorial! I’ve been making quite a few bots lately, so I’ll be posting more about how to create bots and the different feature you can use. Let me know if you have any questions.