eNode
eNode is a simple wrapper around DNode and upnode functionality, simplifying creation and operation of DNode Servers and Clients.
Why
I found that DNode used some confusing patterns and made certain operations complicated, so I wrapped them up in an 'easier' interface.
Features and Differences from DNode
- eNode makes a sharp distinction between Clients and Servers.
- Automatic buffering of requests and reconnections. Upnode is configured automatically between Servers and Clients.
- Servers & Clients don't fire callbacks/events until they recieve the remote's API.
- Unlike Dnode, you don't necessarily need to define the remote API inside the lexical scope of the connection handler to gain access to the remote api and connection. All API calls get passed the remote API & connection, as the final argument of each api call.
- Servers automatically keep a list of connected Clients accessible
as
server.connections
. - The
shutdown
function on Servers and Clients simplifies closing connections. Using DNode, it is a multi-step dance to know when a connection is actually closed. - DNode on 0.6.x doesn't serialize Error objects very usefully (it serializes them as
{}
). eNode findsError
objects in your remote callback data, serializing them to a human readable format. You can override theserializeError
method to expose more or less information.
Usage
Create a Server with an API
An API can be made up of Functions that take callbacks or
Javascript primitive types e.g (Numbers, Strings, Plain Objects, Arrays)
As with DNode, properties on prototypes and return values are ignored.
// Define an API for our Server var api = { // do something with info // stupid api } // Create Server with the supplied API// and listen for client connections on port 3000var server = api
Creating a Client and connecting to a Server
// Connect to server running on port 3000var client = enode
Executing Server API methods from a Client
// Execute remote method 'sendInfo' on Serverclient // you can also access the server's methods via the client's `remote` property (oncethe client has emit 'ready' or the callback has run:client
Creating a Client that exposes an API and connecting to a Server
var client // Create a client apivar api = { client } client = enode
Calling the Client's methods from a Server
// 'connect' event is fired each time a Client connectsserver
Shutting down Clients & Servers
server client // 'shutdown' events will also fireserver client
Passing Errors
// Create a Server with a `makeError` methodvar server = { // send an error back to the client } var client =
Example
// server.jsvar enode = var api = { ) } var server = api serverserver
// client.jsvar enode = var api = { } var client = api client
Todo
- Implement/expose middleware
- More usage examples