hapijs / nes

WebSocket adapter plugin for hapi routes
Other
503 stars 87 forks source link

handling subscriptions after message #167

Closed finedays closed 7 years ago

finedays commented 7 years ago

Hi fellas,

I am trying to implement a logic where when one client is connected to the socket and emits message, the server publishes this to another subscribed client.

I managed to do that if I am using the onMessage option directly while registering the plugin, but if I am trying to use external function, I cannot access the server.publish method.

const handleSocketMessages = require('./handlers/socketHandler').handleSocketMessages
..........
server.register(
{
    register: Nes,
    options: {
      auth: {
        type: 'direct',
        route: 'jwt',
        isSecure: false
      },
      onMessage: handleSocketMessages
    }
  }, (err) => { Hoek.assert(!err, err)

  server.subscription('/subscriptions/{id}')
  server.route(router)

  server.start((err) => {
    Hoek.assert(!err, err)
    console.log('Server started at:', server.info.uri)
  })
})

The handler is:

const handleSocketMessages = function (socket, message, next) {
  switch (message.message) {
    case 'get-drivers-around-me': {
      <socket/server/this/anything>.publish('/subscriptions/1', message)
    }
  }
}

I am skipping the other parts of the code, as I think it is not relevant.

What am I missing?

hueniverse commented 7 years ago

Pass the server to the handler and return a function. Basic JS function factory with argument binding.

finedays commented 7 years ago

Thank you for stating the obvious 💯 Even late night dumbness is not an excuse sometimes :) Thanks once again.

hueniverse commented 7 years ago

No worries.

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.