Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

A guest post by Pedro Teixeira, a geek, programmer, freelancer, and entrepreneur. He is the author of some Node.js modules, the Node Tuts screencast show, some books about Node.js and overall fervent proclaimer of the Node.js creed. He is also the Co-founder and Partner of The Node Firm, and the organizer of the Lisbon JavaScript Conference.

In my last article on applied websockets, I showed you how to create a bi-directional communication channel between a Node.js web server and a browser using sock.js.

On top of that stream we created a bi-directional event stream that let one peer emit events to the other and also let it listen for events emitted by the remote side. We also created a cannonical to-do application using Angular.js.

There is an alternative to this form of communication. For many years, the Remote Procedure Call (RPC) has been one of the most used means of making nodes talk to each other. In RPC there are two intervenients: the server, which hosts a series of procedures, and a client, which calls these procedures. In most RPC implementations, the client calls these procedures as if it was doing local calls, making the code look natural.

In Node.js with the browser you can use this module named rpc-stream to create a RPC channel between a server and a client. You can also make two of these channels on top of a duplex channel (like a sock.js websocket stream) to make the calls bi-directional: not only can the browser invoke procedures on the server, the server can also invoke procedures on the client by its own initiative.

In this post, we’re going to take the To-do application example we built in the previous post and modify it so that it uses an RPC Stream instead of an Event Stream.

For your reference, the final code is in this Github repo.

The Client Web Socket Factory

First, we need to install the rpc-stream module locally:

Then we need to change some client code, which will establish the bi-directional RPC streams over the websocket stream (the bottom of browser/js/app.js):

Here, we are requiring the rpc-stream module and using it to create an RPC stream that provides a given remote interface to the remote part. We’ll see how this interface is specified. This also provides a list of remote functions (inside the remotes argument) that we’ll be able to call on the server as if they were local calls.

The RPC constructor creates a duplex stream that we store on the client variable, which we can now pipe to and from the raw websocket stream provided by sock.js.

The Client List Controller

Now, we need to change the Angular list controller to export this remote interface so that the server can invoke it, and to wrap the server remote interface so that it can be called.

Export the remote interface

Here is the top part of the list controller where we specify the exported remote interface and implementation and store it in a local client variable:

This object says which procedures the browser will be exposing to the server and the functions that will be used to implement that.

Each procedure function takes two arguments:

  1. args – containing an array of the arguments
  2. cb – containing a callback function to be called when the procedure is done or results in an error

Specify the remotes

Next, we need to specify which remote procedures we will be calling on the server:

Use the Websocket factory

Now we can pass these arguments to the Websocket factory:

When the final callback function gets invoked, the controller has a server argument passed in, into which we can invoke the remote procedures as if they were local.

Scope bindings

Now, we need to specify how the controller reacts to user input, by specifying some functions bound to the scope:

Browserify it

Now you can run bin/browserify to generate the bundle that gets served to the browser:

The Server

Now, the server list controller needs to do a similar thing that we did on the browser, creating the RPC stream based upon the procedure spec and implementation, and pipe that to and from the websocket.

It also needs to specify which procedures it expects from the browser so that it is able to call them (websockets/list.js):

The room

For this to work, we just need to change the rooms implementation. The rooms were like an echo chamber that served to bind together browsers that were watching the same list so that they were all notified at the same time of changes performed to it.

Before, the notifications were done using remote events, but now these notifications will be done with plain JavaScript calls that will trigger the respective procedure on the browser. For this to happen, we need to change the room broadcast function to the following (websockets/rooms.js):

Start it up

After browserifying the browser code, you can now fire up the server:

You can then connect to http://localhost:8000 and create a new list. You can open another browser window with the same URL and watch the changes propagate in real time.


In this post, we covered how to use Node.js and the rpc-stream module to create an RPC stream on top of any duplex stream. With it, you can create a bi-directional RPC stream between a browser and a websocket server, effectively enabling two peers to communicate with each other by invoking remote procedures.

See below for some Node.js resources from Safari Books Online.

Read these titles on Safari Books Online

Not a subscriber? Sign up for a free trial.

In just 24 sessions of one hour or less, Sams Teach Yourself Node.js in 24 Hours will help you master the Node.js platform and use it to build server-side applications with extraordinary speed and scalability. Using this text’s straightforward, step-by-step approach, you’ll move from basic installation, configuration, and programming all the way through real-time messaging between browser and server, testing and deployment. Every lesson and case-study application builds on what you’ve already learned, giving you a rock-solid foundation for real-world success!
Node.js is a powerful and popular new framework for writing scalable network programs using JavaScript. Professional Node.js begins with an overview of Node.js and then quickly dives into the code, core concepts, and APIs. In-depth coverage pares down the essentials to cover debugging, unit testing, and flow control so that you can start building and testing your own modules right away.
This book shows you how to transfer your JavaScript skills to server side programming. With simple examples and supporting code, Node Cookbook talks you through various server side scenarios often saving you time, effort, and trouble by demonstrating best practices and showing you how to avoid security faux pas.
Node Web Development gives you an excellent starting point straight into the heart of developing server side web applications with node. You will learn, through practical examples, how to use the HTTP Server and Client objects, the Connect and Express application frameworks, the algorithms for asynchronous execution, and use both SQL and MongoDB databases.

About the author

Pedro Teixeira is a geek, programmer, freelancer, and entrepreneur. Author of some Node.js modules, the Node Tuts screencast show, some books about Node.js and overall fervent proclaimer of the Node.js creed. He is also the Co-founder and Partner of The Node Firm, and the organizer of the Lisbon JavaScript Conference.

Tags: Angular.js, Bi-Directional, Node.js, Remote Procedure Call, RPC, RPC Stream, sock.js, WebSockets,

Comments are closed.