Skip to content

edjafarov/socker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

socker

express like API for websockets over engine.io

with socker you can

  • 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

how

setup

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(err, data){
  // err contains error object if it was thrown
  // data is a response data
})

add middlewares

server.sock.use(logger);
function logger(socket, data, next){
  // 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()
}

add routes

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(socket, data, next){
  // socket.params['id'] contains id from the route
  // data is a data sent
  socket.json({room:"name", id: 100});
}

handle errors

server.sock.use(function(err, socket, data, next){
  if(err){
    socket.json({err:err, code: 500})
  }
})

mantainers

Eldar Djafarov

MIT

About

CRUD over engine.io websockets

Resources

Stars

Watchers

Forks

Packages

No packages published