Is it possible to have "thread" local variables in Node?

Now that more than a year has passed since I originally asked this question, it finally looks like we have a working solution in the form of Async Hooks in Node.js 8.

https://nodejs.org/api/async_hooks.html

The API is still experimental, but even then it looks like there is already a fork of Continuation-Local-Storage that uses this new API internally.

https://www.npmjs.com/package/cls-hooked


Yes, it is possible. Thomas Watson has spoken about it at NodeConf Oslo 2016 in his Instrumenting Node.js in Production (alt.link).

It uses Node.js tracing - AsyncWrap (which should eventually become a well-established part of the public Node API). You can see an example in the open-source Opbeat Node agent or, perhaps even better, check out the talk slides and example code.

Tags:

Node.Js