birdofpreyru / react-native-static-server

Embedded HTTP server for React Native
https://dr.pogodin.studio/docs/react-native-static-server
Other
135 stars 21 forks source link

Question about support for multiple server instances #107

Open Ldoppea opened 3 months ago

Ldoppea commented 3 months ago

Hi,

First, thanks for maintaining this project.

In my project we'll soon need to handle multiple http servers running on different ports. For now we are using a very naive NanoHTTP implementation (that don't support multiple servers yet) and we are considering switching for your library as it seems more future-proof.

In your documentation and code, you specify that it is not possible to run multiple native instances at the same time.

https://github.com/birdofpreyru/react-native-static-server/blob/55edd510f8fe9bc94c157c383c6609292b82b230/README.md?plain=1#L556-L562

https://github.com/birdofpreyru/react-native-static-server/blob/55edd510f8fe9bc94c157c383c6609292b82b230/android/src/main/java/com/drpogodin/reactnativestaticserver/ReactNativeStaticServerModule.kt#L19-L24

My comprehension from those comments is that the limitation is because it is not implemented yet, but it should be technically possible with some work. Is that correct?

If yes, do you have any plan for this feature? Would you accept some PRs if we try implementing it?

birdofpreyru commented 3 months ago

My comprehension from those comments is that the limitation is because it is not implemented yet, but it should be technically possible with some work. Is that correct?

Technically possible, but it will require serious work on the core Lighttpd code, and also it will complicate further maintenance of my Lighttpd fork up-to-date with the upstream changes (because of larger differences between two code-bases). The crux of the problem is that the upstream Lighttpd aims to be build and run as a standalone executable, thus its code uses global variables and functions to manage server state, and assuming that if multiple servers are executed on the same machine, the executable will be run as multiple separate processes — no problem there. In our (React Native) case, we have to build & use it as a shared or static library (depending on the platform) within RN app. That's why we rely on custom Lighttpd fork — I did minimal possible code updates to expose necessary library interfaces and stuff; but it would require further updates / testing to allow multiple server instances within the library/process to keep their isolated states & run without interfering with each other.

If yes, do you have any plan for this feature?

I don't have such plan, unless somebody finances it, or does the work themselves.

Would you accept some PRs if we try implementing it?

I accept PRs, but I am a bit hesitant in this case, as this problem implies complex changes, which might be not easy to review and understand at a glance, but scary to merge in without reviewing, understanding, and testing.

we'll soon need to handle multiple http servers running on different ports

Most importantly, do you really need it? I haven't tried myself yet, but if I recall correctly, Glenn (the upstream Lighttpd maintainer) once told me there should be no problem to configure a single Lighttpd server instance to bind to multiple addresses / ports at the same time, serving different content for each of them. And Lighttpd is supposed to be quite efficient and optimized (to be used for large public web servers), so presumably you should not run into performance limitations in whatever scenario you have for an embedded server within a RN app.

Ldoppea commented 3 months ago

Thanks for this answer :)

Most importantly, do you really need it?

Maybe yes, maybe not. We are looking for a solution to display different local web apps with fully isolated cookies and local storage. At first we did search for a way to use subdomains on localhost, but this seems not to be possible to configure from inside an app.

Using different ports is another solution we are currently investigating.

I haven't tried myself yet, but if I recall correctly, Glenn (the upstream Lighttpd maintainer) once told me there should be no problem to configure a single Lighttpd server instance to bind to multiple addresses / ports at the same time, serving different content for each of them. And Lighttpd is supposed to be quite efficient and optimized (to be used for large public web servers), so presumably you should not run into performance limitations in whatever scenario you have for an embedded server within a RN app.

I did not know about that capability, I'll try to find how to use it 👍 If I find a solution I'll post it here

gstrauss commented 3 months ago

lighttpd 1.4 has always supported binding to multiple ports. It simply needs to be configured.


@birdofpreyru https://github.com/birdofpreyru/lighttpd1.4 would be much cleaner and easier to maintain as a commit or two on top of upstream lighttpd master branch, on which you occasionally pulled upstream and rebased your patches, rather than messy merges. The majority of your changes are to src/CMakeLists.txt, the introduction of src/library-wrap.h, and your development debug trace (some of which might be removed).

Aggregate changes from the lighttpd-1.4.76 tag: git diff --stat lighttpd-1.4.76 git diff --ubw lighttpd-1.4.76