1.0.0 • Published 6 years ago

socket.io-manager v1.0.0

Weekly downloads
22
License
MIT
Repository
github
Last release
6 years ago

socket.io-manager

Manage your socket events easily.

Usage (example)

As first step, let's install socket.io and socket.io-manager:

npm i socket.io socket.io-manager -S

Or:

yarn add socket.io socket.io-manager

Now we need socket server. Create a file called index.js.

// index.js
import socketIO from 'socket.io';

const io = socketIO(9090);

It is time to use socket.io-manager.

Create a file and name it foo.js and import SocketEvent from socket.io-manager and then set up the socket event manager like below.

// foo.js
import { SocketEvent } from 'socket.io-manager';

let socket = new SocketEvent();

// default namespace is '/', if you want change, use SocketEvent.namespace method.

socket
.name('foo')
.middleware(
  (next, { socket }) => text => {
    text === 'you can' ? next() : socket.emit('error', 'I can not do it');
  }
).handler(({ socket }, nsp) => text => {
  nsp.emit('foo', text);
});

now let's back to our index.js and import our socket event manager. we can connect io and our socket events together with connect function provided by socket.io-manager.

// index.js
import socketIO from 'socket.io';
import { connect } from 'socket.io-manager';
import foo from './foo';

const io = socketIO(9090);

// we can connect io to our socket routers with connect function
connect(io, [foo]);

It's done. we can organize our socket like this now. I hope you enjoy it.

API

class SocketEvent

Usage

let socket = new SocketEvent();

Methods

  • namespace(namespace): set namespace. default value of namespace is /, if you don't want to change namespace don't call this method.
    • namespace: type String, required.
  • name(name): set event name for this routers.
    • name: type String, required. event name.
  • middleware(middlewares): add middlewares for this event.

    • middlewares: type Array, required. middlewares.

      How to write a middleware ? it's simple. first look at this example.

      export default (next, { shared, socket, nsp, io }) => (user, msg) => {
        socket.user = user;
        msg = msg + user.name;
      
        next();
      }

      first, it takes 4 arguments, next calls next middleware or handler. socket obviously is socket object. nsp is current namespace scoket.io object. io is main socket.io object. shared is an Object which is created and shared when event is fired till event gets done.

      then it return a function that this function take some arguments. these are event data. for example if in client side I run this code, socket.emit('foo', { name: 'ali' }, 'hey there'), the second function take 2 arguments based on what I've sent to the event. user argument is { name: 'ali' } and msg is hey there.

      that's all.

  • handler(handler): it's main socket handler.

    • handler: type Function, required.

    it looks like middleware function with little difference, it doesn't have next argument.

connect

Usage

connect(io, sockets);

Parameters

  • io: socket.io server class.
  • sockets: type Array, required. array of socket events created by SocketEvent class.

applyMiddleware

if you want to add same middlewares as head middlewares in socket events, it's better to use this function. this function add middlewares before all middlewares in socket events.

Usage

applyMiddleware(middlewares, socketsEvents);

Parameters

  • middlewares: type Array, required. array of middlewares to apply on socket events.
  • socketEvents: type Array, required. array of socket events.

Example

suppose that we have 2 middlewares and one socket event named foo. now we want add these two middlewares in foo socket event. in code language it will be something like this:

applyMiddleware([ middleware1, middleware2 ], [ foo ]);
1.0.0

6 years ago

0.9.0

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.3

6 years ago

0.7.0

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.7

6 years ago

0.5.1

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.0.0

6 years ago