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:
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).
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?
2 Answers 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.
-
2 is definitely preferable. One option you could consider is having a PRNG with a specific known seed modded to large rand of ports.JimmyJames– JimmyJames05/14/2018 13:30:24Commented May 14, 2018 at 13:30
-
The known seed PRNG along with the exclusion list would indeed make this more efficient.laurent– laurent05/14/2018 19:13:50Commented May 14, 2018 at 19:13
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.
-
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.laurent– laurent05/14/2018 19:11:26Commented May 14, 2018 at 19:11
-
3mDNS 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.Jörg W Mittag– Jörg W Mittag05/14/2018 19:34:47Commented May 14, 2018 at 19:34