Gossip API
Gossip API is a Node.js module used to create synchronized RESTful Socket-based JSON API services from MongoDB data models. Gossip is based on a combination of technologies that primarily consist of MongoDB, Mongoose, Restify, and Socket.io.
Server Usage
Order of the Day - Get Node.js and MongoDB installed, familiarize yourself with NPM and Mongoose and begin building APIs.
Project
This is pretty much how any Node.js project is setup (note: use "server.js" as your npm entry point).
[~] mkdir myapi && mkdir models && cd myapi[~/myapi] git init && npm init && npm install mongoose gossip-api
Model(s)
If you are new to the concept of schemas head over to the Mongoose Schema Docs to learn more.
// models/thing.jsmodule{ return mongoose;};
Server
Gossip uses Restify to serve up REST services because that is exactly what Restify was designed to do (and it does it really well).
// server.jsvar config modelPath server;config = port: 5000 mongo: 'mongodb://localhost:27017/gossip?w=1' ;modelPath = __dirname + '/models';server = config modelPath;server;
Note: If you're already familiar with Express then Restify won't be a big jump, check out the Restify Docs to see what I mean.
Run
You just built a synchronized RESTful Socket-based JSON API service on top of MongoDB. Congratulations!
[~/myapi] node serverlistening on 5000
RESTful API
The RESTful (Representational State Transfer) API provides a request-response connection between the client and server that allows the client to issue requests as needed. This is most ideal in high latency situations where real-time communications are unnecessary (and probably overkill).
RESTful Routes
These are the routes that you should use whenever possible.
purpose | usage | description |
---|---|---|
list | GET /things | Get a list of all documents in the "things" collection |
create | POST /things | Create a new document in the "things" collection |
get | GET /things/:id | Get a specific document from the "things" collection |
update | PUT /things/:id | Update a specific document in the "things" collection |
delete | DELETE /things/:id | Delete a specific document in the "things" collection |
Curl Examples
# GET /things curl -i -H 'Accept: application/json' http://localhost:5000/things # POST /things curl -i -H 'Accept: application/json' -X POST -d 'name=value' http://localhost:5000/things # GET /things/:id curl -i -H 'Accept: application/json' http://localhost:5000/things/abcdef0123456789 # PUT /things/:id curl -i -H 'Accept: application/json' -X PUT -d 'name=value' http://localhost:5000/things/abcdef0123456789 # DELETE /things/:id curl -i -H 'Accept: application/json' -X DELETE http://localhost:5000/things/abcdef0123456789
Non-RESTful API
If you, for some reason, are unable to use the PUT and DELETE HTTP methods then you may use these non-restful routes as an alternative.
purpose | usage | description |
---|---|---|
list | GET /things | Get a list of all documents in the "things" collection |
create | POST /things | Create a new document in the "things" collection |
get | GET /things/:id | Get a specific document from the "things" collection |
update | POST /things/update/:id | Update a specific document in the "things" collection |
delete | GET /things/delete/:id | Delete a specific document in the "things" collection |
Curl Examples
# GET /things curl -i -H 'Accept: application/json' http://localhost:5000/things # POST /things curl -i -H 'Accept: application/json' -X POST -d 'name=value' http://localhost:5000/things # GET /things/:id curl -i -H 'Accept: application/json' http://localhost:5000/things/abcdef0123456789 # POST /things/update/:id curl -i -H 'Accept: application/json' -X POST -d 'name=value' http://localhost:5000/things/update/abcdef0123456789 # GET /things/delete/:id curl -i -H 'Accept: application/json' http://localhost:5000/things/delete/abcdef0123456789
Socket API
The Socket API provides a persistent connection between the client and server allowing both parties to send data to one another at any time. This is most ideal in low latency situations where real-time communications are a must.
Client Usage
You'll want to swap localhost:5000 with your own server address, but you get the idea.
Socket Methods
The socket methods mirror the RESTful routes as closely as possible to make going back and forth as seamless as possible.
purpose | usage | description |
---|---|---|
list | things.list(callback) | Get a list of all documents in the "things" collection |
create | things.create(obj, callback) | Create a new document in the "things" collection |
get | things.get(id, callback) | Get a specific document from the "things" collection |
update | things.update(doc, callback) | Update a specific document in the "things" collection |
delete | things.delete(id, callback) | Delete a specific document in the "things" collection |
on | things.on(state, callback) | Get notified when the state of a document in the "things" collection changes |
JavaScript Examples
// listthings; // createthings; // getthings; // updatethings; // deletethings; // on('created')things; // on('updated')things; // on('deleted')things;
License
Licensed under the MIT license