npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-socket.io

v1.0.5

Published

Implementación de socket io para hapi js

Downloads

543

Readme

Hapi-socket.io CI status

Implementacion de socket.io para hapijs .

Requirements

  • Hapi js v17 +

Installation

$ npm install hapi-socket.io --save

Usage

const hapiSocketIo = require('hapi-socket.io')

async () => {
  await server.register({
    plugin: hapiSocketIo,
    options: {
        auth: 'jwt', //jwt Strategy
        socketoptions: {
            //Adicionar las opciones necesarias, el plugin tiene las opciones por defecto de socket.io las cuales puede ver en https://socket.io/docs/server-api/#new-server-httpserver-options
        }
    }
  })
  ...
}

Using hapi-socket.io

  server.route({
    method: "GET",
    path: "/user/getUser",
    config: {
      auth: false,
      handler: handlers.getUser,
      description: 'Example description',
      notes: 'Example notes',
      plugins: {
        'hapi-swagger': {
          payloadType: 'form'
        },
        'hapi-socket.io': {
          event: 'getUser',
          emit: handlers.emitUser
        }
      },
      tags: ['api'],
      validate: {
        query: {
          idUser: Joi.string().required()
        },
        headers: Joi.object({
          'authorization': Joi.string().required()
        }).unknown()
      }
    }
  })
}

//handler function respond to the same customer
module.exports.emitgetUser = async function(context, h){
  var socket = context.socket

  const result = await context.server.seneca.actAsync({role: "user", cmd: "getUser", payload : context.data, token : ""});
  socket.emit('testemit', result)
  return h.continue;
}

//handler function to emit to all customer
module.exports.emitgetUser = async function(context, h){
  var io = context.io

  const result = await context.server.seneca.actAsync({role: "user", cmd: "getUser", payload : context.data, token : ""});
  io.sockets.emit('testemit', result)
  return h.continue;
}

//handler function to emit to others customer
module.exports.emitgetUser = async function(context, h){
  var socket = context.socket

  const result = await context.server.seneca.actAsync({role: "user", cmd: "getUser", payload : context.data, token : ""});
  socket.broadcast.emit('testemit', result)
  return h.continue;
}

//The context variable contains...
{
  io: io,
  socket: socket,
  event: event,
  data: data,
  server: ser
}

Client Example

var io = require('socket.io-client');
var ioClient = io.connect('http://localhost:9001/', {
  transports: ['websocket']
});
console.log('ioClient', ioClient);
ioClient.on('connect', function() {
  console.log('connect');
  ioClient.emit('event', { test: 1 });
});
ioClient.on('testemit', msg => console.log(msg));
ioClient.on('disconnect', function() {
  console.log('disconnect');
});

hapi-socket.io options

auth: text string with the defined security strategy, false, in case of not requiring authentication socketoptions: you can define the options of the socket io library

Auth options

We have added, the option to validate with the authentication strategy defined in the server, This option is valid only with the socket configuration that allows the sending of extraHeaders.

// Client example
this.socket = io.connect(URL_SERVER, {
  forceNew: true,
  jsonp: false,
  transports: ['polling'],
  transportOptions: {
    polling: {
      extraHeaders: {
        Authorization: token // heres go your token
      }
    }
  }
});

please do the registry after define your auth strategy

//Auth API with JWT
server.auth.strategy('jwt', 'jwt', {
  key: jwtkey,
  validate: validate,
  verifyOptions: {
    ignoreExpiration: false, // do not reject expired tokens
    algorithms: ['HS256'] // specify your secure algorithm
  },
  urlKey: true
});

server.auth.default('jwt');

await server.register({
  plugin: hapiSocketIo,
  options: {
    auth: 'jwt',
    socketoptions: {}
    //add the necessary options, the plugin has the default options of socket.io which you can see in https://socket.io/docs/server-api/#new-server-httpserver-options
  }
});

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT