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

Allow binding websocketPort to port #1799

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

cnuss
Copy link
Contributor

@cnuss cnuss commented Jun 18, 2024

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 AbstractHttpServer superclass which sets up commonalities between the various HttpServer classes most likely due to copy-paste of them over time
  • Has helper functions so the HttpServer classes are more uniform
  • Uses the Lambda class to maintain a map of which HttpServer is on which port
  • Uses the Map() to have a single HttpServer created once and is the sole source of truth for all HttpServer instances
  • Updated HttpServer in the websocket class override superclass functions to bind on the correct server.

Finally:

  • In the event the websocketPort is set to the same value as port, the @connections API is hosted on the HttpServer created by the Lambda class. Less is more, right?!

How Has This Been Tested?

  • All existing tests work after the refactor
  • The websocket-oneway and websocket-twoway integration tests were copied with the new shared port configuration and also work.

@cnuss cnuss changed the title Feat/shared websocket Allow binding websocketPort to port Jun 18, 2024
@cnuss
Copy link
Contributor Author

cnuss commented Jun 18, 2024

hi @dherault and @DorianMazur could you take a look at this PR? Thanks!

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

Successfully merging this pull request may close these issues.

None yet

1 participant