serverless-offline
serverless-offline copied to clipboard
Allow binding `websocketPort` to `port`
Description
In the following serverless.yml configuration:
custom:
serverless-offline:
port: 3000
websocketPort: 3000
The following error occurs at startup:
✖ Unexpected error while starting serverless-offline server on port 3000: { Error: listen EADDRINUSE: address already in use ::1:3000
at Server.setupListenHandle [as _listen2] (node:net:1898:16)
at listenInCluster (node:net:1946:12)
at GetAddrInfoReqWrap.doListen [as callback] (node:net:2116:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:111:8)
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::1',
port: 3000 }
Motivation and Context
In a lot of HTTP + Websocket services, the Websocket and HTTP server can share the same port since they are on different protocols.
This PR:
- Introduces a
AbstractHttpServersuperclass which sets up commonalities between the variousHttpServerclasses most likely due to copy-paste of them over time - Has helper functions so the
HttpServerclasses are more uniform - Uses the
Lambdaclass to maintain a map of whichHttpServeris on which port - Uses the
Map()to have a singleHttpServercreated once and is the sole source of truth for allHttpServerinstances - Updated
HttpServerin thewebsocketclass override superclass functions to bind on the correct server.
Finally:
- In the event the
websocketPortis set to the same value asport, the@connectionsAPI is hosted on theHttpServercreated by theLambdaclass. Less is more, right?!
How Has This Been Tested?
- All existing tests work after the refactor
- The
websocket-onewayandwebsocket-twowayintegration tests were copied with the new shared port configuration and also work.
hi @dherault and @DorianMazur could you take a look at this PR? Thanks!
Thanks for the PR. I like the idea of the PR, but I feel it introduces too many changes at once. If you'd be willing to reduce its size somehow, I'd be more enclined to merge it.
Closing. I'm developing a framework outside of serverless and no longer need this