In my last post I talked about the great annyang library by Tal Ater, which is a keyword detector and abstraction layer for Chrome’s continuous speech recognition system.

Annyang is a great client side JavaScript library for detecting a spoken keyword and performing a specific callback function. But what if you want the same functionality server side?

Use annyang-node of course.

Annyang-node is a partical port of annyang to Node.js. It’s basically a JavaScript library for keyword based initiation of callback functions, which stays consistent with annyang’s syntax.

Just like annyang, annyang-node has splats, variables, and optional words and phrases.

The major difference is that annyang-node does not yet provide voice recognition.

Usage

Annyang-node works nearly the same as annyang, but is slightly different.

Because annyang is always used in one browser and thus by one user there only ever needs to be one global annyang object. This works great for client side code. However, annyang-node is used on the server, and can be used by many users at the same time. For example, in a WebSocket server, where each users has a separate callback attacked to their socket. This requires annyang-node to be instantiated each time before it’s used.

var Annyang = require('annyang');

var annyang = new Annyang();

You can then start defining commands to be used as callback functions.

var commands = {
    'show tps report': function() { 
        // do something 
    }
};

After that you must pass the commands object to the annyang-node instance.

annyang.init(commands);

And finally you can trigger one of the commands using the trigger method. This can be done at any point after the annyang-node object is instantiated.

annyang.trigger('show tps report');

Of course in a real application you would pass in a variable to the trigger method.

Why use annyang-node?

When writing voice driven applications you sometimes want to initiate callback functions on the client, and sometimes on the server. Generally you want to use the same syntax for both sides, as it makes it much easier to jump back and forth between environments and much easier to share routing logic.

Annyang is the client side solution and annyang-node is the server side solution. They work very well together.