socker

express like API CRUD on engine.io

socker

express like API for websockets over engine.io

  • use middlewares to process incoming data
  • give a response on incoming "requests"
  • use routing to structure your apps
  • handle errors in the same way as in express

backend:

var engine = require('engine.io');
var socker = require('socker');
 
var app = require('express')();
var server = http.createServer(app);
server.listen(nconf.get('server:port'));
server = engine.attach(server);
socker(server);
server.on('connection', function(socket){
  socker.attach(socket);
});
 

frontend:

var socket = require('engine.io')('ws://localhost');
var sockerClient = require('socker-client');
sockerClient(socket);
socket.serve('CREATE /api/rooms', {name:"testName"}, function(errdata){
  // err contains error object if it was thrown 
  // data is a response data 
})
server.sock.use(logger);
function logger(socketdatanext){
  // socket is a socket object 
  // socket object have .json method to send a response 
  // data - the data object sent with `request` 
  console.log(data);
  next()
}

With routings you can create meaningful websocket based API

server.sock.when('CREATE /api/rooms', checkRoom, createRoom);
server.sock.when('READ /api/room/:id', getRoom);
function getRoom(socketdatanext){
  // sicket.params['id'] contains id from the route 
  // data is a data sent 
  socket.json({room:"name", id: 100});
}
server.sock.use(function(errsocketdatanext){
  if(err){
    socket.json({err:err, code: 500})
  }
})

Eldar Djafarov