Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
238 lines (163 sloc) 8.33 KB

Meta Age

The Meta Aspect of Node.js

I've been exploring since few months now; the last Buzzy programming environnement : Node.Js .

And as a Developer fighting for a kind of "hyper generic" : universal exchange network, and as a Hacker wishing to exceed limitations.

I'm quite interested in the "Meta" aspect of programming environnement, the way you could dynamically push forward the limit of a system.

And I found with Node.JS a really prolific activity around the development of such abstracting layers (I described below) that I never found before in any other programming environnement.

Pushed by the HTML5 wave, javascript development ( ECMAScript 5 ) and community is becoming really active and exciting : Kinect interfacing, Face recognition, Drone control, p2p Webcam Streaming, 3D data flow editor / Physics engine, IDE, etc...

Below are some problematic to push forward programming limitations and really good Libraries that illustrate how to find a nice solutions using Node.js and/or CoffeeScript :

How enabling you to create your own semantic programming language : DSL (Domain Specific Language) ?

CoffeeScript is a meta language over Javascript, taking the best from ruby & python = a really readable code with less verbosity

Javascript compilation can be executed on runtime in both client ( browser ) and server ( node.js )

Bored by syntax like Javascript :

var logFunc;
logFunc = function() {
  var args;
  args = 1 <= arguments.length ? Array.prototype.slice.call(arguments, 0) : [];
  return console.log.apply(console, args);
};

A nice alternative CoffeeScript :

logFunc = (args...) -> console.log args...

CoffeeKup is an HTML DSL over CoffeeScript as a template engine ( used in Zappa below )

Writing your template in CoffeeScript, your HTML is becoming less verbose, and block more evident

    html ->
      head ->
      body ->
        h1 @title or "Untitled"
        div "#divID.className", ->
           span style: "font-weight: bold", 
             "Content"
           text " of my Div"

Generated HTML :

    <html>
      <head>
      </head>
      <body>
        <h1>Untitled</h1>
        <div id="divID" class="className">
          <span style="font-weight: bold">Content</span>
     of my Div    </div>
      </body>
    </html>

In which way you can write and distribute the same code for browser / server / node ?

Zappa enable you to write clear CoffeeScript Web App in only one file.

# Will be compiled and served as static Javascript
@client '/index.js': ->
    #Client Scope
    alert "I'm at : #{window.location.href}"
# Server HTTP Routes
@get '/': ->
   @render 'index', 
       title: "Hello"
       myContainerContent: "World!"
# View rendered by default template engine CoffeeKup
@view 'index': ->
   h1 @title
   div '.myContainer', ->
      @myContainerContent
  • "Universal WebSocket" client / server communication using Socket.io
#Server Scope events receiver
@on 
   connection: ->
     @emit welcome: "client"
   "welcome.back": -> console.log 'welcome ack'
@client '/index.js': ->
  # Client Scope
  # Socket Connection
  @connect()
  # Client event receiver
  @on welcome: (me) ->
    alert 'server say welcome to #{me} !'
    @emit 'welcome.back'
@client '/index.js': ->
  # Client Hash Route
  @get '#/route': -> alert 'client routes!'
# Code will be executed on server and served as static javascript for browser, nice for creating a bootlooader
@shared '/shared.js': ->
   root = window ? global
   alert if window? then "I'm in the Browser" else "I'm in node"
   root.sum = (x, y) -> x + y

It is a really nice way to wrap main libs clearly, see the Zappa.coffee annotated source and hack it ;)

Is it possible to switch from a programming paradigm to an other ?

Node.js use the async programming paradigm, like Javascript in the Browser. which really boost performance on I/O for a given CPU core

I came to node.js seeing my Javascript Semantic Web library on Browser side (using HTTP SPARQL Query) taking 10x less time to load a graph by recursive loading that the ruby ORM : Spira I was using on ruby backend server

ruby had a solution to write asynchronous code synchronously it has introduced Fibers in v1.9, which had an implementation on Node.js using node-fibers. But it is not working on browser side ... unless you discover an other wonderfull lib Streamline.js

Streamline.js is Asynchronous Javascript for dummies, you write your code using the "_" (underscore) as callback and chain your operations, replacing this :

lineCount = (path, callback) ->
  fs.readFile(path, "utf8", (err, data) ->
    if err
      callback err
      return
    callback null, data.split('\n').length

by this :

lineCount = (path, _) ->
  fs.readFile(path, "utf8", _).split('\n').length

The code could run with node-fibers in Node.js or generated to javascript asynchronous code, see the Streamline.js Code Generator

How to create meta architecture

By breaking communication barriers, with a distributed evented network

Hook.io a distributed EventEmitter network

hookA = new Hook name: "a"
hookB  = new Hook name: "b"

hookA.on "*::sup", (data) ->
   console.log "#{@event} #{data}"
hookB.on "hook::ready", ->
  hookB.emit "sup", "dog"

hookA.start()
hookB.start()

By decoupling components and building a network architecture on top like in flow based programming

Noflo a flow based programming library

Creating compnents with named input and output streams, linked together by graphs.

A File count line network definition in FBP

# FBP Network Syntax
'somefile.txt' -> SOURCE Read(ReadFile) OUT -> IN Split(SplitStr)
Split() OUT -> IN Count(Counter) COUNT -> IN Display(Output)
Read() ERROR -> IN Display()

I have writted a small wrapper around this lib enabling you to create component like this :

Tweet = (data_in, out_url, out_message) ->
  if data_in().entities?.urls?
    for url in data_in().entities.urls
      out_url.send url.expanded_url 
  out.message.send data_in().text 

How to code in a meta environnement

Usually the more important tool for a developer is the IDE ( distributed vim :) ), for this Javascript / Node.JS is pushing your working environement in the browser with project like :

Cloud9 a Web RAD IDE with Node.js cloud plugged environement

JSFiddle a Web IDE for Client code prototyping / testing libs

CoffeeXP a CoffeeScript live coding experiments

Hosting is neither a problem with cloud hosting service / techno

Nodester an Open source cloud hosting / Node.js

Nodejitsu scalable Node.js clouds

So my dream of a Live Stream Social Coding Cloud, has been pushed some steps forward :)