Skip to content

18sg/shet-client.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A client library for SHET. It can be used in Node.js, or in web browsers using Socket.IO.

Get

npm install shet-client

Use

In Node.js

var shet = require("shet-client").connect();

In the browser

This uses Socket.IO, so it's probably easiest to get that working first (see http://socket.io/#how-to-use).

Install as above, then use Browserify to generate a single library file:

browserify -r shet-client -o public/shet.js

Include this in your page, then add the something like this to the server:

# after "io = require('socket.io').listen(app)"
require("shet-client").listen_socket(io);

...and something like this to the client:

# after "var socket = io.connect(...);"
var shet = require("shet-client").connect(socket);

This creates a SHET client, that can be used exactly as in Node.

The API

Rather than using callbacks for everything, this library uses Q to provide 'deferred values' or 'promises', as in the Twisted version.

var c = connect([options])

Create a new client.

options is an optional object that may contain the following keys:

  • host: The host to connect to. Defaults to $SHET_HOST or localhost.
  • port: The port to connect to. Defaults to $SHET_PORT or 11235.
  • ping_interval: The interval in milliseconds for sending pings to the server. Defaults to 30,000. If 0, no pings are sent.

var c = connect(socket)

Create a new client connected to the given Socket.IO channel.

listen_socket(socket, [options])

Accept connections on the given Socket.IO channel, and pass traffic to the SHET server (which is resolved as above).

c.add_action(path, callback)

Add an action. When the action is called, callback will be called with the appropriate arguments, and it's return value will be returned to the caller. If callback returns a Q promise, it will wait for this to resolve before returning, as expected.

c.add_event(path)

Add an event. This returns an object with a raise attribute. Calling this with any number of arguments raises the event with the given arguments.

c.add_prop(path, get_cb, set_cb)

Add a property. get_cb should take no arguments and return the desired value of the property. set_cb should take a single argument, and set the property.

c.watch(path, callback)

Watch an event. The callback will be called with the arguments of the event.

c.call(path, args...)

Call an action with some arguments. This returns a promise, which will resolve to the return value of the action.

c.get(path)

Get a property. This returns a promise, which will resolve to the return value of the action.

c.set(path, value)

Set a property to value. This returns a promise, which will resolve when the set completes.

Todo

  • Examples
  • Setting the root directory
  • Removing properties/events/actions.
  • Event added/removed events?
  • Testing

About

MIT licensed; see LICENSE.

Built with CoffeeScript, Node.js, Socket.IO, Q, Browserify, recon, and Lazy.

About

SHET client library for use in node and the browser.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published