Skip to content

Emscripten POSIX socket support #2261

@theo-dep

Description

@theo-dep

I am prototyping an Emscripten httplib::Client build using the Emulated POSIX TCP Sockets over WebSockets tutorial.
For the work, see this commit: 49b526c1ec4a8c09bba0f5163c004024b5130a8c.

Are you interested in this kind of development?

I’m not entirely convinced it’s a good idea myself, but the Emscripten Fetch API is much more difficult to use and less documented compared to cpp-httplib.

With my prototype, I can build and run the following small example in a browser:

// main.cpp
#include <httplib.h>
#include <print>

int main()
{
#ifdef __EMSCRIPTEN__
    httplib::emscripten_init_websocket("ws://localhost:8080");
#endif

    httplib::Client cli("http://yhirose.github.io");

    auto res = cli.Get("/hi");
    std::println("Status: {}", res->status);
    std::println("Body: {}", res->body);

    return 0;
}

The build command and execution with emrun are as follows:

em++ -std=c++23 -o index.html main.cpp -lwebsocket.js -sPROXY_POSIX_SOCKETS -pthread -sPROXY_TO_PTHREAD -sMEMORY64 -sEXIT_RUNTIME

This approach requires the Emscripten bridge socket server, websocket_to_posix_proxy, or a Node.js WebSocket server implementation. I haven’t yet figured out how to deploy this in a production environment, hence my questions about the prototype.

I started a functional project demonstrator.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions