Monthly Archives: May 2017

Publishing ServiceBus message from Node (Electron)

This is a handy little module that wraps up the Azure ServiceBus code in a tight little package. I don’t claim any of it as original work, since I have pulled from more sources than I can remember.

It is a work in progress, but I wanted to document what I have so far.

Code for azHelper.js

"use strict";

var azure = require("azure-sb");
var sbConnect = "Endpoint VALID CONNECTION STRING HERE...";
var sbService = azure.createServiceBusService(sbConnect);

function publishMessage(messageLabel, message, category) {
  if (!category) {
    category = 1000;
  console.log("Publishing message", messageLabel);

  var sbMessage = {
    body: JSON.stringify(message),
    brokerProperties: {
      Label: messageLabel
    customProperties: {
      Category: category

  sbService.sendTopicMessage("rapids", sbMessage, sbCallback);

var sbCallback = function(err) {
  if (err) {
    console.log("ERROR publishing message", err);
  } else {
    console.log("message published");

module.exports = {
  publish: publishMessage

And the calling code…

var azHelper = require("./azhelper.js");

var contentFoundMessage = {
    fileName: fileName,
    baseName: baseName

azHelper.publish("ContentFound", contentFoundMessage, 1100);

Unique object names for S3

When creating buckets or objects in S3, I wanted an easy way to create a unique name for each object.

The names to be as short and human readable as possible. (Guids have their place, but I don’t want to look at them).

The Aws Rules for Bucket Naming lists the following guidelines.

  • Bucket names must be at least 3 and no more than 63 characters long.
  • Bucket names must be a series of one or more labels. Adjacent labels are separated by a single period (.). Bucket names can contain lowercase letters, numbers, and hyphens. Each label must start and end with a lowercase letter or a number.
  • Bucket names must not be formatted as an IP address (e.g.,
  • When using virtual hosted–style buckets with SSL, the SSL wildcard certificate only matches buckets that do not contain periods. To work around this, use HTTP or write your own certificate verification logic. We recommend that you do not use periods (".") in bucket name

NPM shortid

In the past, I had settled on using the NPM package shortid, but it did not seem to be returning the first character randomly. Based on my recent listen to the Pluralsight course File Storage Using AWS S3 and Glacier: Developer Deep Dive, I want my names to be random, so Amazon can better spread the load out on the hardware.

Custom Id Generation

The bulk of this code comes directly from Tom Spencer’s post – Short ID Generation in JavaScript. His use case was a little different, but you should read it for a more generic approach. I wanted something that took the date into consideration, so I hacked his approach.

Finally, some code

Here is my s3helper.js module

"use strict";

module.exports = (function() {
  const moment = require("moment");

  // Just to ensure we don't start a name with a number, we
  // use alpha only for the prefix.
  const LIST_ALPHA = "abdegjkmnpqrvwxyz";
  const LIST_ALL = "23456789" + LIST_ALPHA;

  const ALL_LENGTH = LIST_ALL.length;
  const BUCKET_ID_LENGTH = 8;
  const OBJECT_ID_LENGTH = 3;

  var getPrefix = function() {
    var retVal = LIST_ALPHA.charAt(
      Math.floor(Math.random() * LIST_ALPHA.length)
    return retVal;

  function getbucketName() {
    var retVal = getPrefix();
    for (var iLoop = 0; iLoop < BUCKET_ID_LENGTH; iLoop++) {
      retVal += LIST_ALL.charAt(Math.floor(Math.random() * ALL_LENGTH));
    return retVal;

  function getobjectName() {
    var retVal = getPrefix();
    for (var iLoop = 0; iLoop < OBJECT_ID_LENGTH; iLoop++) {
      retVal += LIST_ALL.charAt(Math.floor(Math.random() * ALL_LENGTH));

    retVal += "-" + moment().diff("2014-08-26T08:01", "seconds");
    return retVal;

  return {
    bucketName: getbucketName,
    objectName: getobjectName

Using the code is pretty easy.

console.log("s3", s3Helper.bucketName(), s3Helper.objectName());

Which gives me this output in the console.

Dropping files in Electron (with Angular)

I started with a clone this project.

My goal was to find an easy way for a user to drop files into an Electron app.

Electron makes this pretty easy by providing the File Object. However, that code was not working for me, since I was working in an Angular app.

This Post on StackOverflow helped me solve the issue. (This Gunter guy is awesome!)

StackOverflow post

The only small hurdle was finding the right place to put the dropscript.js. This is what I ended up with.