data.io
Bidirectional data syncing via Socket.IO
Example
On the server:
var io = ;var data = io; var messages = data; var store = {};var id = 1; messages; messages; messages;
On the client:
Resources
Resources are stacks of composable middleware functions that are responsible for handling sync requests from the client and responding appropriately. Each middleware layer is a function that accepts a request and response object (as well as a function that can be called to continue execution down the stack). A middleware layer will generally either modify the request context and pass control to the next layer or respond to the client with some kind of result or error.
For example, we could add logging middleware to a resource:
var messages = data; messages; messages;
Middleware can be selectively applied to particular actions by specifying them in your calls to use
. If a request's action does not match a particular layer then that layer will be skipped in the stack. For example, we might want to authorize requests on create, update and delete actions:
messages; messages;
Request
When clients initiate a sync with the server a request object is created and passed through the middleware stack. A request object will contain the following properties:
action
- the type of sync action being performed (i.e. create, read, update, etc.)data
- any data provided by the clientoptions
- additional options set by the clientresource
- the resource handling the syncclient
- the Socket.IO client that initiated the request
Middleware functions can use the request object for storing arbitrary data via the get(key)
and set(key, value)
functions:
messages; messages;
Response
The response object provides mechanisms for responding to client requests. It exposes two function send
and error
that can be used for returning results or errors back to the client.
Events
When a client connects to a particular resource a connection
event is emitted on the resource. This can be used to perform any initialization, etc.
messages;
If your connection event handler needs to perform an async operation, call this.async()
from within your callback. The connection will not handle any requests until all async operations have completed without error.
messages;
When a response is sucessfully sent back to the client a sync
event is emitted on the resource and a sync object is provided.
messages;
The sync object contains the following properties:
client
- the client that initiated the syncresource
- the resource that handled the syncaction
- the sync action performedresult
- the result returned to the client
And provides these methods:
notify(emitter, ...)
- emit a sync event on the given emittersstop()
- stop the default notification (syncs are broadcast to all clients by default)
For example:
messages;
Client
The client-side component provides a thin wrapper around Socket.IO for syncing data to the server and listening for sync events triggered by the server.
var conn = ;var messages = conn;
Make requests to the server with a resource's sync
function:
sync(action, [data], [options], callback)
- perform the specified action optionally sending the given data and request options, callback takes an error and a result
messages;
Listen to syncs from the server with a resource's subscribe
function:
subscribe([action], ..., callback)
- listen for syncs happening on the server optionally passing the actions to which the client should listen, callback accepts a result and action
// Listen to all actionsmessages; // Listen to specific actionsmessages;
The client library can also be used server-side via the socket.io-client module:
var socket = ;var conn = ;var messages = conn;// etc.
Install
npm install data.io
Test
npm test
License
Copyright (C) 2013-2015 Scott Nelson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.