background preloader

NodeJS

Facebook Twitter

Blog.gvm-it.eu, Callback conventions in node.js, how and why. Callback conventions in node.js, how and why When first confronted with node.js, you are not only presented with a completely new programming environment. You also encounter what is often referred to as callback hell accompanied by weird unfamiliar programming patterns. One of these is the way node treats callback functions.

The following post explains the conventions that node.js uses for its callback patterns (referred to as Continuation-passing style) and how you should implement them in order to comply. The first argument is an error object Node expects - almost - all callback functions to accept an Error object as the first argument. If no error occurred, the first argument should be null.

There is a reason why this is a usefull pattern: Imagine, you have a chain of asynchroneous functions that were to execute one after the other. Now think something went wrong in the first function, i.e. the file cannot be read. Pass error objects, not strings When to apply these pattern fully. The cross-browser WebSocket for realtime apps. What is Socket.IO? Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript. Server Client How to use Installing npm install socket.io Using with Node HTTP server For this example, simply run `npm install socket.io` Server (app.js) Client (index.html) Using with the Express 3 web framework Express 3 requires that you instantiate a `http.Server` to attach socket.io to first: Using with the Express web framework You can serve normal pages and AJAX requests with Express, and attach your socket.io server For this example, simply run `npm install socket.io express` Sending and receiving events.

Socket.IO allows you to emit and receive custom events. Storing data associated to a client Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session. Restricting yourself to a namespace. Sending volatile messages. Desktop Mobile. Express - api reference. Homepage - Node Tuts - Node.js Free screencast tutorials. Mongoose ODM v3.1.2. The Node Beginner Book » A comprehensive Node.js tutorial. Node.js cloud products and services | Nodejitsu Inc. NodeCloud - Node.js resources. How To Node - NodeJS.