Madhusudhan Srinivasa

about

node-notifier

One of the most common things while developing API's or web applications is that you want to notify your users about some new stuff. node-notifier is a simple module that makes notifications easy.

Installation

$ npm install notifier

or include it in package.json

Usage

For email notifications, you have to use (jade) templates to write the email body. By default it uses postmarkapp to send emails (in the upcoming versions you'll be able to configure this). If you plan to not use jade, you can override the processTemplate method to use templating language of your choice.

var notifier = new Notifier({
  APN: false, // apple push notifications
  email: true,
  actions: ['comment', 'like'], // should be the name of the template files
  tplPath: require('path').resolve(__dirname, './templates'), // path to the directory where all the templates reside
  postmarkKey: 'POSTMARK_KEY'
});

var comment = {
  to: 'Tom',
  from: 'Harry'
};

var options = {
  to: 'tom@madhums.me',
  subject: 'Harry says Hi to you',
  from: 'harry@madhums.me',
  locals: comment // should be the object containing the attributes used in the template
};

notifier.send('comment', options, function (err) {
  if (err) return console.log(err);
  console.log('Successfully sent Notifiaction!');
});

For APN (apple push notifications), notifier uses parse-sdk. Set the APN key to true and provide parseAPI appId and app secret. You can either do

var notifier = new Notifier({
  APN: true, // apple push notifications
  actions: ['comment', 'like'], // should be the name of the templates
  parseAppId: 'APP_ID',
  parseApiKey: 'MASTER_KEY',
  parseChannels: ['USER_5093a266180b779762000005']
});

or if you have already an instance of Notifier

notifier.use({
  APN: true,
  parseChannels: ['USER_5093a266180b779762000005'] // user Id of the user in your system
})

// var options = { ... }

notifier.send('comment', options, function (err) {
  if (err) return console.log(err);
  console.log('Successfully sent Notifiaction!');
});

Resources:


localStorage manager (lm.js)

localStorage manager (lm.js) is a simple wrapper around localStorage with which you can store, query and perform CRUD operations on collections and documents in an easy way.

Initialize your app

var todoapp = new lm('todoapp');

Create a collection

// create collection
var list = todoapp.create('todos');

or create an initialized collection

var todosList = [
  { id: 1, name: 'shopping' },
  { id: 2, name: 'washing' }
];
var list = todoapp.create('todos', todosList);

Add records to collection

// create collection
var list = todoapp.create('todos');

// add records to collection
list.add({ name: 'shopping' });

Chain them

var archived = todoapp
  .create('archived')
  .add({ name: 'shopping', tag: 'outside' })
  .add({ name: 'eating', tag: 'kitchen' })
  .add({ name: 'bathing', tag: 'inside' })
  .add({ name: 'cleaning', tag: 'kitchen' });

Remove a collection

// remove a collection
todoapp.remove('todos');

Retrieve a collection

// get collection
var archived = todoapp.get('archived');

Find within a collection

archived.find({ tag: 'kitchen' }, function (docs) {
  console.log(docs);
  // output:
  // [{ name: 'eating', tag: 'kitchen' }, { name: 'cleaning', tag: 'kitchen' }]
});

Update a document within a collection

archived.find({ tag: 'kitchen' }, function (docs) {
  docs.find({ name: 'eating' }, function (records) {
    var doc = records[0]; // { name: 'eating', tag: 'kitchen' }

    doc.update({ name: 'cutting' });

    console.log(doc); // { name: 'cutting', tag: 'kitchen' }
  });
});

Remove a document within a collection

archived.find({ tag: 'kitchen' }, function (docs) {
  docs.find({ name: 'eating' }, function (records) {
    var doc = records[0]; // { name: 'eating', tag: 'kitchen' }

    doc.remove();

    console.log(doc.name);
    // undefined
  });
});

To use it, just source lm.js in your app.

Tests

You can see the test results on travis-ci

Limitations

Localstorage has a limitation of 5MB, so if you want to use offline data storage with files etc, indexedDB would be a good option.

Resources:


Getting started with AngularJS

I wrote a blog post couple of days back about the pain I was facing in developing client side web apps (with backbone). I tried angular and it tries to solve the problems I was having.

As I elaborated in my previous post, the biggest issue (with backbone) was wiring these single components and to make them work together, angular solves this using dependency injection and data binding. It's a win (for me).

I was looking for something which

  1. increases productivity
  2. reduces the code and hence easier to maintain
  3. follows conventions
  4. gives you separation of concerns
  5. follows test first approach

I sat for two days and I went through series of videos and blog posts, I am listing them here.

  1. Ask HN: AngularJS vs Backbone vs Ember?
  2. SO: AngularJS vs Backbone.js [closed]
  3. 8 Tips for AngularJS Beginners
  4. Youtube: Building AngularJS TODO app tutorial
  5. Youtube: Building AngularJS twitter search
  6. SO: What is Dependency Injection?
  7. Youtube: Angular.js intro + Dependency Injection
  8. Backbone and Angular: Demystifying the myths
  9. 3 reasons to choose AngularJS for your next project
  10. Youtube: AngularJS Best practices

I started with the tutorial and parallelly I watched/read the above links. Also the documentation is good enough to help you through. There are some nice examples you can play around with.

Angular also comes with a boilerplate (angular-seed), just for YOU!

If you are not happy with how backbone is working out for you, you should seriously give it a try.


trade-offs of purely client side development approach

I've been developing products on backbone.js on the front end for past couple of months and I feel that the development is rather slow when compared to the server side (have you felt the same?). Here I am talking about fairly large applications in which you are working with collaborators.

I want to address a few things in this post

  1. Why go with purely client side approach?
  2. Trade-offs of client side approach

Why go with purely client side approach?

  1. Responsiveness
  2. RESTful interface

One of the main reasons I feel like going with this approach is the responsiveness. If you use tools like grunt.js, you can just have 1 css file, 1 js file and 1 image sprite (yes there is an awesome module called node-spritesheets) and 1 html file. The whole app resides on the browser and its super fast and responsive! With this you'd also get to build a neat RESTful interface. These are the only two main advantages I see.

Trade-offs of client side approach

  1. No hard and fast conventions
  2. Development time is rather slow
  3. Hard to maintain

When I say convention think about the conventions that you'd follow while developing an application server side and then compare it with the conventions that you'd follow with the client side approach (forget about the designs for a moment).

One of the differences between client side approach and server side approach is that - you don't get to use tools that make your development faster. When I say tools here, it means stuff like generators etc. And there is no convention based approach, like if you place a file somewhere, it would not automatically perform something out of the box. So you need to manually wire up all the connections between these components, which also makes it a bit hard to maintain.

What I love about the js community is its a free world! You can write code however you want! (not literally) There are numerous advantages! The drawback here being the lack of conventions on how to use them with each other. Because of this, there are chances that you might end up doing a particular task in a very complex and time consuming way (as opposed to the way you'd have done if there was a convention) Of-course you can bring some conventions on a project level, that's what happens all the time but even there, its a bit hard to maintain the code - coz not everyone would write code of the same quality, simplicity. Then you end up doing code reviews.

What do you think? Do you feel the same? I would like to know about your experiences.

Tools and libs I've been using:

I haven't tried any other libs like angular or meteor...


Install/update node.js in just one command!

A simple script to install/update the much frequently updated node.js platform.

Here's what you have to do to install/update node.js and npm to the version you want in just 1 command

$ curl https://raw.github.com/gist/3791075 | sh -s 0.8.10

Its a bit difficult to remember the url everytime you want to update, so you can create a simple alias function which does the job. Here's how

Put the below in your ~/.bash_profile

function update-node {
  version=$1
  curl https://raw.github.com/gist/3791075 | sh -s $version
}

Now you can simply run update-node 0.8.10 !

Oh and btw, it will install node to ~/local/node directory.

Resources: