Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Last active January 4, 2016 13:08
Show Gist options
  • Select an option

  • Save kvendrik/120104a6cff8ae836f45 to your computer and use it in GitHub Desktop.

Select an option

Save kvendrik/120104a6cff8ae836f45 to your computer and use it in GitHub Desktop.
Sockets as a service flow
# 1. Setup
//creates an instance and connects
var socket = new SillySockets(io, {
    baseUrl: '<sockets-server-address>',
    appId: 'ABCDE12345',
    debug: true,
    tags: ['superawesomeclient', 'iphone']
});
#2 Emit Events
  1. Client: socket.emit('superawesomeevent', { tags: ['iphone'], data: { dataAwesomeLevel: 'over9000' } })
  2. Sockets server: On emit from client check APP_ID, event name and tags then POST with data to URL from DB
#3 Listen for Events
  1. Server: POST <sockets-server-address>/<APP_ID> --payload { event: 'superawesomeevent', action: 'emit', tags: ['iphone'], data: { dataAwesomeLevel: 'over20000' } }
  2. Sockets server: emit superawesomeevent to all clients connected to APP_ID connection with given tags
  3. Client: socket.on('superawesomeevent', function(data){ console.log(data); //{ dataAwesomeLevel: 'over20000' } })

A host-it-yourself easy to use and setup service for languages that don't play nice with sockets

Now and then you want to or have to use a language that doesn't play nice with sockets, thats when you use SillySockets. A host it yourself sockets service that can be controlled with simple HTTP requests.

Setup

git clone [URL]
cd SillySockets
make install
make run

How to use

Listening for Events

Server
./db create superapp
./db add-event superapp set-awesomeness http://your-server-url/set-awesomeness
Client
io.connect('[SILLYSOCKETS_URL]', { query: 'APP_ID=superapp' });
io.emit('set-awesomeness', { level: 'over9000' });
Your Server
app.post('/set-awesomeness', function(req, res){
    console.log(req.body);
    //{ level: 'over9000' }
});

Emitting Events

curl -H "Content-Type: application/json" -X POST
-d '{ event: 'new-awesomeness', action: 'emit', data: { level: 'over90000' } }' <SILLYSOCKETS_URL>/<APP_ID>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment