creates socket.io event maps for resourceful.
The socketful project removes the process of writing boilerplate socket.io event mapping code for interacting with resourceful resources. socketful uses reflection to reflect a socket.io server interface that maps all socket.io events needed to perform basic real-time CRUD operations with resourceful. socketful also has the ability to expose additional arbitrary remote resource methods in socket.io
npm install socketful
var resourceful = require'resourceful'Creature = resourcefuldefine'creature';Creatureproperty'type' String default: "dragon" ;Creatureproperty'life' Number default: 10 min: 0 max: 20 ;
To use socketful as a Flatiron plugin you will have to:
Here is a code example of using socketful as a Flatiron plugin: https://github.com/flatiron/socketful/blob/master/examples/app.js
To use socketful as a stand-alone server you will have to:
Here is a code example of using socketful as a stand-alone server: https://github.com/flatiron/socketful/blob/master/examples/server.js
var io = require'socket.io-client';var socket = ioconnect'';var user = id: 'bob' type: 'dragon' ;socketemit'creature' 'create' useriferrconsole.log'Error \n\n' + JSONstringifyerr true 2;elseconsole.log'Created creature! \n\n' + JSONstringifyresult true 2;;
socketful will map all
Resourceful methods in the following signature:
serveronresource action payload callback;
socketemit'creature' 'create' id: 'bob'console.log'created: ' bob;;
Socket Resource Action Payload Callback Resource Methodsocket.emit('creature', 'create', data, callback) => Creature.create()socket.emit('creature', 'get', data, callback) => Creature.get()socket.emit('creature', 'all', data, callback) => Creature.all()socket.emit('creature', 'update', data, callback) => Creature.update()socket.emit('creature', 'destroy', data, callback) => Creature.destroy()
The socket.io server will delegate all incoming
Creature events to the resource and respond back with the appropriate result.
In many cases, you'll want to expose additional methods on a Resource through socket.io outside of the included CRUD operations:
socketful has built in support for easily exposing arbitrary remote resource methods.
Consider the example of a
Creature. We've already defined all the socketful CRUD events, but a Creature also needs to eat!
Simply create a new method on the
Creature resource called
callbacknull 'I have been fed';
feed method is consider private by default, in that it will not be exposed to the web unless it's set to a
remote function. To set a resource method to remote, simply:
Creaturefeedremote = true
It's easy as that! By setting the
feed method to remote, the following events will exist in the
socketemit'creature' 'feed' data callback => Creaturefeed
There are several ways to provide security and authorization for accessing resource methods exposed with socketful. The recommended pattern for authorization is to use resourceful's ability for
after hooks. In these hooks, you can add additional business logic to restrict access to the resource's methods.
TL;DR; For security and authorization, you should use resourceful's