Giter Club home page Giter Club logo

koa-socket's Introduction

Koa-socket

Sugar for connecting socket.io to a koa instance

Installation

npm i --save koa-socket

Example

var koa = require( 'koa' );
var socket = require( 'koa-socket' );

var app = koa();

app.use( ... );

socket.start( app );

socket.on( 'join', function( data ) {
  console.log( 'join event fired', data );
});

app.server.listen( process.env.PORT || 3000 );

Middleware and event handlers

Middleware can be added to sockets in much the same way as it is added to a koa instance.

var app = require( 'koa' )();
var socket = require( 'koa-socket' );

app.use( ... );

socket.use( function *( next ) {
  var start = new Date();

  yield next;
  console.log( 'elapsed:', new Date() - start );
});

socket.use( ... );

app.server.listen( 3000 );

Middleware context references both the data sent along with the socket event and the socket itself.

// From the client
socket.emit( 'event', {
  ua: navigator.userAgent,
  time: new Date()
});

// Listening on the server
socket.on( 'event', function( packet ) {
  // Id now exists on the packet thanks to middleware
  // Although in this trivial example it can be referenced on the context
  console.log( packet.id );
  console.log( this.id );
});

socket.use( function *( next ) {
  // This will fail
  console.log( this.data.id );

  yield next;

  // It'll work here though
  console.log( this.data.id );
});

socket.use( function *( next ) {
  this.data.id = this.socket.id;
});

In the above example the event handler will get executed after the middleware and the context is shifted so that this references the socket and its data is passed as the packet parameter.

License

ISC

koa-socket's People

Contributors

mattstyles avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.