Is it possible shared an event loop (libuv) between uWebSockets and my backend ? #1833
-
Hi, I am trying to build an asynchronus http server with uWebSockets that would allow me to query my backend before responding to the http call.
Is it possible to run both the http server and the backend communication on the same event loop ? Or should I run them on separate event loops and find a way to communicate between them ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Yes that's how it's intended to be used. Sharing event loop is exactly how
you do it proper
Den tis 14 jan. 2025 12:28Théo Issarni ***@***.***> skrev:
… Hi,
I am trying to build an asynchronus http server with uWebSockets that
would allow me to query my backend before responding to the http call.
I have a code base based on libuv (wrapped with
https://github.com/skypjack/uvw) that allow me to communicate with my
backend and I am trying to integrate it with the http server.
I am trying use the same event loop for both the http server and the
backend communication but I am having some issues.
int main() {
std::shared_ptr<uvw::loop> loop = uvw::loop::get_default();
uv_loop_t* uvLoop = loop->raw();
uWS::Loop::get(uvLoop);
uWS::App app;
app
.listen(3630,
[](auto* listen_socket) {
if (listen_socket) {
fmt::print("Listening on port {}\n", 3630);
}
})
.get("/health", [&](auto* res, auto* /*req*/) {
// Want to be able to interact with our backend before responding/ending the call
res->end("OK");
});
std::shared_ptr<uvw::timer_handle> timer = loop->resource<uvw::timer_handle>();
timer->on<uvw::timer_event>([&loop](const auto&, uvw::timer_handle& handle) { loop->stop(); });
timer->start(uvw::timer_handle::time{100000}, uvw::timer_handle::time{0});
loop->run(); // Does not seems to "run" the http server
// app.run(); // Does not seems to "run" the uv_loop
app.close();
}
Is it possible to run both the http server and the backend communication
on the same event loop ? Or should I run them on separate event loops and
find a way to communicate between them ?
Any advice on how to achieve this would be greatly appreciated.
—
Reply to this email directly, view it on GitHub
<#1833>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A2NMOMPYJ3AWCUU2KCHQQOD2KTYHJAVCNFSM6AAAAABVEVRHOOVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZXHAZDEOBZGU>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
I figured my problem. I was compiling my POC with the
WITH_LIBUV=1
flag and linking with the uWesbSockets lib instead of compiling the uWesbSockets lib with theWITH_LIBUV=1
and linking it to my binary.So the loop used by the uWebSockets lib was not the libuv loop, but the default one.
If someone read my discution and want to combine an uWebSockets server with an existing libuv loop, here is an example :