Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate with SocketIO #53

Open
averri opened this issue Dec 10, 2019 · 5 comments
Open

How to integrate with SocketIO #53

averri opened this issue Dec 10, 2019 · 5 comments

Comments

@averri
Copy link

averri commented Dec 10, 2019

I'm trying to integrate the cls_hooked with SocketIO, using the Feathersjs framework:

const socketio = require('@feathersjs/socketio')
const tracer = require('../tracer')
const sync = require('feathers-sync')

module.exports = app => {
  // 'socket.feathers' is the same object as the connection
  // in a channel. 'socket.request' and 'socket.handshake' contains information
  // the HTTP request that initiated the connection.
  app.configure(socketio(io => {

    // https://docs.feathersjs.com/api/socketio.html#params
    io.use( (socket, next) => {
      tracer.run(() => {
        tracer.set('ip', socket.conn.remoteAddress)
        next()
      })
    })

  }))
}

The tracer.js:

const cls = require('cls-hooked')
const tracer = cls.createNamespace('app')
module.exports = tracer

... but the context is lost, it's not possible to get the 'ip' property from service functions.

@ppatel890
Copy link

@averri Any luck figuring out how to setup with feathers?

@averri
Copy link
Author

averri commented Jan 31, 2020

Hi @ppatel890, unfortunately I have abandoned the use of cls-hooked, I have re-engineered the application code to do not rely on cls-hooked.

@ppatel890
Copy link

@averri - checking back in, any guidance on how you can have a requestId present throughout the entire socket 'request'?

@averri
Copy link
Author

averri commented May 19, 2020

Hi @ppatel890, Nodejs 13 has a solution for this problem:
https://nodejs.org/docs/latest-v13.x/api/async_hooks.html#async_hooks_class_asynclocalstorage

In order to have a variable associated with the context of the current request it needs the support of AsyncLocalStorage in the SocketIO middleware.

The AsycLocalStorage has similar features of this library, so it's recommended to move to AsyncLocalStorage.

@malixsys
Copy link

malixsys commented Mar 7, 2023

Hi @ppatel890, Nodejs 13 has a solution for this problem: https://nodejs.org/docs/latest-v13.x/api/async_hooks.html#async_hooks_class_asynclocalstorage

In order to have a variable associated with the context of the current request it needs the support of AsyncLocalStorage in the SocketIO middleware.

The AsycLocalStorage has similar features of this library, so it's recommended to move to AsyncLocalStorage.

@averri This library already uses async_hooks on Node 8+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants