Repository navigation
Expose the HTTP server to module loaders (ContainerRegistrationKeys.HTTP_SERVER) #17783
Gustavitory
started this conversation in
Feature Requests
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.
Summary
Register the
http.Servercreated bymedusa startin the container under a new key,ContainerRegistrationKeys.HTTP_SERVER, and pass it to module loaders as a default dependency. This lets modules and plugins attach to the server (WebSocket, Socket.IO, GraphQL subscriptions, etc.) without patching Node internals.Problem
In
packages/medusa/src/commands/start.tsthe server is created beforeloaders()runs, but it only lives in a local variable (http_). Module loaders have no way to reach it.Today the only workaround is monkey-patching
http.createServerfrominstrumentation.tsto capture the first server Medusa creates. I described this approach in #15299 and we have been running it in production (multi-instance, Socket.IO with the Redis adapter). It works, but it is fragile:instrumentation.tsrunning beforecreateServer, which is an implementation detail.globalThis) to hand the server from the instrumentation hook to the module loader.medusa exec, migrations) through environment variables.Proposal
Add
HTTP_SERVER: "httpServer"toContainerRegistrationKeys.Register the server right after it is created in
start.ts, beforeloaders():Add
ContainerRegistrationKeys.HTTP_SERVERto the default dependencies passed to every module's local container inload-internal.ts, next toLOGGER,CONFIG_MODULE, etc.Type it in
ModuleImplementationsasServer | undefined.Usage
medusa exec, migrations or build, the key is not registered, soserverresolves toundefinedand the loader skips.listen(), so attaching here is safe.Backward compatibility
Purely additive: one new key and one extra registration. No behavior changes for existing apps, modules or plugins.
Implementation
The change is ready on my fork, a 7-line diff plus a changeset, and builds cleanly:
https://github.com/Gustavitory/medusa/tree/feat/http-server-container-key
Open questions
httpServer/HTTP_SERVERa good name, or would you prefer something else?workermode the server is still created (it serves/health). I left it registered there and let modules decide based onprojectConfig.workerMode. Would you rather not register it in worker mode?Follow-up
Once this lands, I'd like to propose an optional realtime module (Socket.IO, configured through
medusa-config, with a Redis adapter for multi-instance), following the same opt-in pattern as the Caching module. I can open a separate discussion for it.I'd like to be assigned to the issue and open the PR.
All reactions