hapi-socketio-router
v1.0.2
Published
A hapi plugin for socket.io that accepts an array of JSON definitions of namespace/events to register with socket.io. Great for modularizing your namespace definitions.
Downloads
2
Readme
hapi-socketio-router
About
A hapi plugin for socket.io that accepts an array of JSON definitions of namespace/events to register with socket.io. Great for modularizing your namespace definitions.
This plugin was inspired by hapi-router by @bsiddiqui. In fact, it borrows from his code. ;) Thank you,bsiddiqui!
Installation
npm install hapi-socketio-router
Usage
First, let's create a directory with registration modules. For instance, if we have socket/chat.js
and socket/something-else.js
, the chat.js
might look like:
socket/chat.js
module.exports = [
{
namespace: "/chat",
events: [
{
event: "connection",
handler: function (socket) {
socket.on('hello', function (data) {
console.log(data);
socket.emit("news", {message:'We got your request on namespace: ' + socket.nsp.name});
});
socket.on('disconnect', function (data) {
console.log('user disconnected from ' + socket.nsp.name);
});
console.log('user connection to ' + socket.nsp.name);
}
}
]
}
];
As you can see, the module is defined as an array of configurations by namespace. This allows you to put all your namespaces into a single file, or you can break them out into individual files.
Now, let's load our events:
var server = new hapi.Server();
server.connection({
host: 'localhost',
port: 3000,
labels: 'primary',
routes: {
cors: {
origin: ['*:*']
}
}
});
server.register(
{
register: require('hapi-socketio-router'),
options: {config: {}, namespaces: 'socket/*.js'}
},
{select: "primary"},
function (err) {
if (err) {
console.log(err);
throw err;
}
}
);
Specification
Valid options
Valid options
which are passed to the plugin include the following. Please see glob for details on glob specific options.
cwd
- (deprecated) Instructs the plugin to change to this directory, otherwise load namespace files from project root +namespaces
path parameter. This is passed toglob
.ignore
- (deprecated) Another glob option.options
- (deprecated) Socket.io options that will be passed to the socket initialization.config
- (optional) Object representing all configuration options.io
- (optional) Socket.IO related configuration.server
- (optional) A Socket.IO object, not yet attached to the Hapi server. By default the plugin creates it's own Socket.IO server.options
- (optional) Options passed to Socket.IO'sServer#attach(srv:http#Server, opts:Object):Server
method. Please see Socket.IO documentation forServer(opts:Object)
.
glob
- (optional) Object representing glob options accepted by this plugin. Those options are:cwd
- (optional) Sets the working directory to search for namespace files. Defaults to process.cwd().ignore
- (optional) Add a pattern or an array of patterns to exclude matches. Defaults to false. See glob'sComments and Negation
documentation.
namespaces
- (required) A glob path to namespace files. Example:socket/*.js
. Also, seedoc/example.js
for a namespace file example.debug
- (optional) Turns on debugging mode. Default output goes to console.log()logger
- (optional) A callback function that overridesdebug
's default console output. Accepts a single string parameter.
Example plugin options
var pluginOptions = {
debug: true,
logger: function(message) {console.log(message);},
config: {
io: {
server: undefined,
options: {
serveClient: undefined,
path: undefined
}
},
glob: {
cwd: undefined,
ignore: undefined
}
},
namespaces: 'server/sockets/*.js'
};
hapiServer.register({register: require('hapi-socketio-router'), options: pluginOptions});
Namespace files
Namespace files are defined as an array of objects.
module.exports = [
{
namespace: "/some-namespace",
events: [
{
event: "connection",
handler: function (socket) {
// YOUR SOCKET STUFF HERE
}
},
{
event: "some-other-event",
handler: function (socket) {
// YOUR SOCKET STUFF HERE
}
}
]
},
{
namespace: "/some-namespace-2",
events: [....]
}
];