Repository navigation
Socket IO on medusa JS #15299
Gustavitory
started this conversation in
Show and tell
Replies: 1 comment
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hey everyone! 👋 Wanted to share a Socket.IO integration for Medusa v2 in case it helps anyone here.
TL;DR: I needed to attach a Socket.IO server to the same HTTP server Medusa creates internally (shared port, CORS, origin), but Medusa doesn't expose that http.Server instance anywhere — loaders only receive expressApp, and the server is created internally inside start.ts.
The only user-controlled hook that runs before http.createServer(...) is the register() function from instrumentation.ts (the same one used for OpenTelemetry). So I monkey-patch http.createServer from there: capture the first server Medusa creates, attach io to it, then restore createServer to the original. Fully working in dev (medusa develop with HMR) and prod, with bearer-JWT auth per actor namespace (/admin, /customer, /employee, /partner, plus a custom /shopper in our case) reusing getAuthContextFromJwtToken from @medusajs/framework/http.
I already raised this with the team and there might be a first-class hook coming, but in the meantime this is the cleanest approach I found. Patch file 👇
A custom Medusa module then runs as a regular module loader, picks up globalThis.__medusaIo, and registers per-actor namespaces with bearer-JWT auth. Happy to share that part too if anyone wants it. Feedback / better ideas welcome! 🙏
All reactions