A bad-ass client for porchetta-server.
$ bower install git@github.com:activecell/porchetta-client.git --save
Or copy index.js and socket.io-client.js to vendor folder.
Assume you started porchetta-server on http://localhost:4000 with npm start.
porchetta = new Porchetta('http://localhost:4000', app.company.id);
# Watch collections
porchetta
.addCollection(app.vendors, 'vendors')
.addCollection(app.tasks, 'tasks')
.addCollection(app.accounts, 'accounts')
.addModel(app.company, 'company')
.addModel(app.user, 'user')
porchetta.on 'connect', ->
console.log('connected to the server')
porchetta.on 'viewers', (viewers) ->
if viewers.length > 1
console.log('Porchetta is active for ' + viewers.length + ' viewers');
else
console.log('Porchetta is inactive');
# Porchetta emits event after every sync with pattern: <name>:<event>
porchetta.on 'vendors:add', (json) -> console.log('new vendor added', json)
porchetta.on 'tasks:change', (json) -> console.log('task changed', json)
porchetta.on 'accounts:remove', (json) -> console.log('account removed', json)In general porchetta-client is wrapper around socket.io-client. Porchetta starts to work when has more than one connected user.
Connect to porchetta server, that started on url, and join selected room.
socketIoOptions is an optional parameter with specific options for socket.io.
Porchetta's instance is event emiiter. Subscribe to specific events with on and once methods
porchetta.on 'connect', ->
console.log('connected to the server')Available events (for more examples, check test suite):
connect- after succesfull connect.viewers- someone connected or disconnected.<name>:<event>- after sync event.
Sync Backbone.Collection events: add, change, remove. Make sure that name is unique identificator.
For collections, which do not have id, you have to define findByJSON method.
Sync Backbone.Model instance through porchetta-server. It tracks only change event. Logic is similar to addCollection.
npm install- to install npm/bower dependenciesnpm test- to run testsnpm start- to start local watch server on http://localhost:7357
