socket.io-manager v1.0.0
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 -SOr:
yarn add socket.io socket.io-managerNow 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: typeString,required.
name(name): set event name for this routers.name: typeString,required. event name.
middleware(middlewares): add middlewares for this event.middlewares: typeArray,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,
nextcalls next middleware or handler.socketobviously is socket object.nspis current namespacescoket.ioobject.iois mainsocket.ioobject. shared is anObjectwhich 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.userargument is{ name: 'ali' }andmsgishey there.that's all.
handler(handler): it's main socket handler.handler: typeFunction,required.
it looks like middleware function with little difference, it doesn't have
nextargument.
connect
Usage
connect(io, sockets);Parameters
io:socket.ioserver class.sockets: typeArray,required. array of socket events created bySocketEventclass.
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: typeArray,required. array of middlewares to apply on socket events.socketEvents: typeArray,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 ]);