Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonastemplestein/813251 to your computer and use it in GitHub Desktop.
Save jonastemplestein/813251 to your computer and use it in GitHub Desktop.
node-object-sync server example
ObjectSync = require 'object-sync'
server = http.createServer()
# Hook an ObjectSync instance up to our server.
# Define a bunch of handlers for CRUD events. The handlers are
# async because they'll likely interact with some kind of database
sync = ObjectSync.listen server,
# a client wants to delete object with id id
destroy: (id, client_sid, callback) ->
console.log "client #{client_sid} has destroyed object #{id}"
callback null # sends events to clients
# a client wants to update an object
update: (obj, client_sid, callback) ->
# sends an error to the client requesting the update and no
# message to all other clients
callback
code: 'invalid_id'
# a client wants to create an object
create: (obj, client_sid, callback) ->
callback null, obj
# a client requests a list of objects
fetch: (ids, client_sid, callback) ->
results = [] # ...
callback null, results
# The following functions let the server pro-actively change things
sync.save obj, (err, obj) -> # ...
sync.destroy obj, (err, obj) -> # ...
sync.fetch ids, (err, objs) -> # ...
sync.update obj, (err, obj) -> # ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment