2

I want to create a service that would be running locally (not accessible from the internet) on a given port, then a browser extension will need to access this service.

My problem is that I cannot know what port will be available in advance so I would need to pick one more or less randomly and I'll need the extension to be able to discover what port it is.

I wonder what would be a good way to implement this?

I thought about two solutions:

  1. Service-side: Pick random ports until one is available and start the service on this port. Extension-side: Scan all the ports and ping them until the service is found (the service will have a /ping end-point and will respond with a specific string that can be recognised).

  2. Service-side: Have a list of pre-selected ports (eg. 8900, 8901, 8902) and find the first one that is available. Extension-side: Scan all the ports in that list until the service is found.

I think none of these solutions is that great. 1. is probably very slow and inefficient. 2. is a bit better but it creates coupling between the app and service, which will both need to know the list of possible ports. Although if this list is ever changed, both service and app will have to be updated at the same time or else the app won't be able to find the service. Also it's an issue if for some reason all the ports in that list are unavailable.

So basically I'm not sure what's the best approach here? Maybe I'm looking at it wrong?

asked May 14, 2018 at 12:00

2 Answers 2

2

I would advise you to do option #2 and the respect the following list of ports: List of port numbers of numbers to avoid.

answered May 14, 2018 at 12:28
2
  • 2 is definitely preferable. One option you could consider is having a PRNG with a specific known seed modded to large rand of ports. Commented May 14, 2018 at 13:30
  • The known seed PRNG along with the exclusion list would indeed make this more efficient. Commented May 14, 2018 at 19:13
6

In "enterprise" systems, service discovery is often handled by the service advertising itself in a well-known location, known as a service registry.

After the service has found an open port and completed startup, it contacts the service registry and provides its connection info. Other software that depend on the service check the service registry on startup for the connection info for the service.

This decouples the components (service and browser extension, in your case) from each other, but does require a 3rd application with static connection info to act as a service registry.

This may be overkill for your use-case, but it may make sense if you already have an application component that could act as the service registry.

answered May 14, 2018 at 13:59
2
  • Thanks but I should have mentioned it's not in an entreprise situation and there won't be a service registry running. It's an open source app that can be installed in any kind of environment without any knowledge of what's already running. Commented May 14, 2018 at 19:11
  • 3
    mDNS combined with DNS-SD can provide a service registry without configuration or prior knowledge. You may know the combination of those two protocols better under the brand name Apple chose to apply to them: Bonjour. Commented May 14, 2018 at 19:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.