Skip to content
Browse files

[examples] Updating examples and README

  • Loading branch information...
1 parent 8f2b111 commit f47c7f0fb9a78ef5ea413a079c2df7a37d7604f4 @Marak Marak committed Aug 24, 2012
Showing with 55 additions and 27 deletions.
  1. +20 −24 README.md
  2. +8 −1 examples/app.js
  3. +1 −1 examples/public/index.html
  4. +26 −1 examples/server.js
View
44 README.md
@@ -1,5 +1,3 @@
-# Experimental / Unreleased
-
# socketful
Reflects [socket.io](http://socket.io) servers from [resourceful](http://github.com/flatiron/resourceful) resources. Can be used as a stand-alone module or as a [Flatiron](http://github.com/flatiron/) plugin.
@@ -18,6 +16,18 @@ Through the removal of this boilerplate code, socketful creates a robust, standa
# Usage
+## Define resource(s)
+
+```js
+var resourceful = require('resourceful'),
+ Creature = resourceful.define('creature');
+
+Creature.property('type', String, { default: "dragon" });
+Creature.property('life', Number, { default: 10, min: 0, max: 20 });
+```
+
+*[additional API documentation for defining resources](http://github.com/flatiron/resourceful)*
+
## As a Flatiron Plugin
To use socketful as a <a href="http://github.com/flatiron/flatiron">Flatiron</a> plugin you will have to:
@@ -48,35 +58,22 @@ server.on(resource, action, payload, callback);
**Example:**
```js
-socket.emit('creatures', 'create', { id: 'bob' } , function(err, bob) {
+socket.emit('creature', 'create', { id: 'bob' } , function(err, bob) {
console.log('created: ', bob);
};
```
```
Socket Resource Action Payload Callback Resource Method
-socket.emit('creatures', 'create', data, callback) => Creature.create()
-socket.emit('creatures', 'get', data, callback) => Creature.get()
-socket.emit('creatures', 'all', data, callback) => Creature.all()
-socket.emit('creatures', 'update', data, callback) => Creature.update()
-socket.emit('creatures', 'destroy', data, callback) => Creature.destroy()
+socket.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.
-## Relational Resources
-
-To define relational data in socketful you will have to:
-
- - Define the relationship in the resource itself using the resourceful `Resource.parent()` API
- - Create a new server based on the resource(s)
-
-socketful will then properly reflect the relational properties of your resources into the socket server.
-
-Here is a simple code example of using socketful with `Albums` and `Songs`: <a href="https://github.com/flatiron/socketful/blob/master/examples/server.js">https://github.com/flatiron/socketful/blob/master/examples/server.js</a>
-
-
-
<a name"remote"></a>
## Exposing Arbitrary Resource Methods
@@ -117,9 +114,8 @@ There are several ways to provide security and authorization for accessing resou
# TODO
- - Full `resourceful` property type support ( numeric, boolean, array, object )
- - Full `resourceful` nested property schema support
- - Better browser / isomorphic support
+ - Cleanup create / extend code for flatiron plugin
+ - Better browser / isomorphic support via resourceful `socketful` engine
- Add ability to specify schemas for remote method argument payloads
- Improve Tests
- Add better error support via `errs` library
View
9 examples/app.js
@@ -15,7 +15,10 @@ var app = module.exports = flatiron.app;
app.resources = {};
app.resources.Creature = fixtures.Creature;
app.resources.Album = fixtures.Album;
+app.resources.Song = fixtures.Song;
+
+app.use(socketful);
app.use(flatiron.plugins.http, {
headers: {
'x-powered-by': 'flatiron ' + flatiron.version
@@ -24,9 +27,13 @@ app.use(flatiron.plugins.http, {
ecstatic(__dirname + '/public')
]
});
+
app.start(8000);
-socketful.createServer([app.resources.Creature], { server: app.server });
+// Socket.io
+// -------------------------------------------------- //
+
+var io = require('socket.io').listen(app.server);
console.log(' > http server started on port 8000');
console.log(' > visit: http://localhost:8000/ ');
View
2 examples/public/index.html
@@ -5,7 +5,7 @@
var socket = io.connect('http://localhost');
var name = prompt('creature name?');
var type = prompt('creature type?');
- socket.emit('creatures', 'create', { id: name, type: type }, function(err, result) {
+ socket.emit('creature', 'create', { id: name, type: type }, function(err, result) {
if(err) {
alert('Error \n\n' + JSON.stringify(err, true, 2));
} else {
View
27 examples/server.js
@@ -6,13 +6,38 @@
*/
var http = require('http'),
+ fs = require('fs'),
fixtures = require('../test/fixtures'),
socketful = require('../lib/socketful'),
resourceful = require('resourceful');
//
// Create a new socket server based on defined resources
//
-var server = socketful.createServer([fixtures.Creature, fixtures.Album]);
+var server = http.createServer(function(req, res){
+ fs.readFile(__dirname + '/public/index.html', function (err, data) {
+ if (err) {
+ res.writeHead(500);
+ return res.end('Error loading index.html');
+ }
+ res.writeHead(200);
+ res.end(data);
+ });
+});
+
+var Book = resourceful.define('book', function () {
+ this.string('title');
+ this.number('year');
+ this.bool('fiction');
+});
+var Author = resourceful.define('author', function () {
+ this.number('age');
+ this.string('hair').sanitize('lower');
+});
+var Creature = resourceful.define('creature', function () {
+ this.string('name');
+});
+socketful.createServer([fixtures.Creature, fixtures.Album, fixtures.Song], { server: server });
+server.listen(8000);
console.log(' > socket server started on port 8000');

0 comments on commit f47c7f0

Please sign in to comment.
Something went wrong with that request. Please try again.