hapipal/boilerplate

it's really not an issue but i need your help

Opened this issue · 6 comments

i am new in hapi and i really like your boilerplate api but i don't know how can i add plugin
for example i want to add hapi-io plugin for socket and i should put this lines
server.register({
register: require('hapi-io'),
options: {
...
}
});

exports.register = function(server, options, next) {

var io = server.plugins['hapi-io'].io;

};

where should i put them

can you help me i really need your help @devinivy

Hey @mostafa-raafat
See how Dogwater is registered in server/manifest.js. You'll have to add your registration in the registrations array. Something like:

registrations: [
    {
        plugin: {
                register: 'hapi-io',
                options: {}
        }
    }
]

thank you @WilliamSWoodruff for reply
i make it like you said but i get an error when i try to put

let io = server.plugins['hapi-io'].io;
    io.emit('connection', function (socket) {
        socket.emit('connected',"Welcome");
    });

in index.js file

let io = server.plugins['hapi-io'].io;
TypeError: Cannot read property 'io' of undefined

The boilerplate is split between a plugin (in lib/) and a server (in server/) to deploy that plugin. If you'd like to register hapi-io in your plugin, simply add a file lib/plugins/hapi-io.js that exports options for hapi-io in the form { options: {/* hapi-io options */} }. haute-couture will then register that plugin for you, and you should be able to access server.plugins['hapi-io'] inside haute-couture's callback in lib/index.js. If you'd like to register hapi-io to your server as @WilliamSWoodruff indicated, I would suggest using server.dependency() (with a callback) within your plugin to ensure hapi-io has been registered before you try to access server.plugins['hapi-io'].

what i want to make to say on connection emit connected
and on add user for example make some thing that's all

routes/user.js look like this

'use strict';
const Joi = require('joi');
const Boom = require('boom');

let addedUser = false;
let userList = [];

module.exports = [
    {
        method: 'get',
        path: '/user',
        config: {
            tags: ['api'],
            plugins: {
                'hapi-io': 'get-users'
            },
            handler: (request, reply) => {
                let io = request.plugins['hapi-io'].io;
                let socket = request.plugins['hapi-io'].socket;
                if (socket) {
                    socket.emit('get-users', userList);
                }
                reply({success: true});
            }
        }
    },
    {
        method: 'post',
        path: '/user',
        config: {
            tags: ['api'],
            plugins: {
                'hapi-io': 'add-user'
            },
            validate: {
                payload: {
                    username: Joi.string().alphanum().min(3).max(30).required()
                }
            },
            handler: (request, reply) => {
                let io = request.plugins['hapi-io'].io;
                let socket = request.plugins['hapi-io'].socket;
                if (socket) {
                    if (addedUser) return;
                    addedUser = true;
                    socket.username = request.payload.username;
                    userList.push({username: request.payload.username});
                    socket.emit('login', {userList: userList});
                    socket.broadcast.emit('user-joined', {
                        username: request.payload.username
                    });
                }
                reply({success: true});
            }
        }
    },
    {
        method: 'delete',
        path: '/user',
        config: {
            tags: ['api'],
            plugins: {
                'hapi-io': 'disconnect'
            },
            handler: (request, reply) => {
                let io = request.plugins['hapi-io'].io;
                let socket = request.plugins['hapi-io'].socket;
                if (socket) {
                    if (addedUser) {
                        for (let i = 0; i < userList.length; i++) {
                            if (socket.username === userList[i].username) {
                                userList.splice(i, 1);
                            }
                        }
                        socket.broadcast.emit('user-left', {
                            username: socket.username
                        });
                    }
                }
                reply({success: true});
            }
        }
    },
];

index.js look like this

'use strict';

const Package = require('../package.json');
const HauteCouture = require('haute-couture')();

exports.register = (server, options, next) => {

    HauteCouture(server, options, (err) => {

        if (err) {
            return next(err);
        }

        let io = server.plugins['hapi-io'].io;
        io.emit('connection', function (socket) {
            socket.emit('connected',"Welcome");
        });

        next();
    });


};

exports.register.attributes = {
    pkg: Package
};

i think i did something wrong i get

socket.on(event, function(data, respond) {
             ^

TypeError: socket.on is not a function
    at /home/spider/Application/Hapi/boilerplate-api/node_modules/hapi-io/lib/routes.js:19:14
    at Array.forEach (native)
    at /home/spider/Application/Hapi/boilerplate-api/node_modules/hapi-io/lib/routes.js:15:12
    at Array.forEach (native)
    at module.exports (/home/spider/Application/Hapi/boilerplate-api/node_modules/hapi-io/lib/routes.js:8:16)
    at Namespace.<anonymous> (/home/spider/Application/Hapi/boilerplate-api/node_modules/hapi-io/lib/index.js:61:5)
    at emitOne (events.js:77:13)
    at Namespace.emit (events.js:169:7)
    at Namespace.emit (/home/spider/Application/Hapi/boilerplate-api/node_modules/hapi-io/node_modules/socket.io/lib/namespace.js:209:10)
    at Server.(anonymous function) [as emit] (/home/spider/Application/Hapi/boilerplate-api/node_modules/hapi-io/node_modules/socket.io/lib/index.js:425:29)
    at /home/spider/Application/Hapi/boilerplate-api/lib/index.js:15:12
    at done (/home/spider/Application/Hapi/boilerplate-api/node_modules/haute-couture/node_modules/haute/node_modules/items/lib/index.js:31:25)
    at /home/spider/Application/Hapi/boilerplate-api/node_modules/haute-couture/node_modules/haute/lib/index.js:133:17
    at iterate (/home/spider/Application/Hapi/boilerplate-api/node_modules/haute-couture/node_modules/haute/node_modules/items/lib/index.js:36:13)
    at done (/home/spider/Application/Hapi/boilerplate-api/node_modules/haute-couture/node_modules/haute/node_modules/items/lib/index.js:28:25)
    at /home/spider/Application/Hapi/boilerplate-api/node_modules/haute-couture/node_modules/haute/lib/index.js:133:17

That seems to be an issue with hapi-io or the way you're using it, which I don't know much about– sorry, don't think we can help you with that!