floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
7.04k stars 494 forks source link

sending data / making a website with sokol? #825

Closed digitalsignalperson closed 1 year ago

digitalsignalperson commented 1 year ago

Can/should sokol be used to make something like a SPA? Are there any examples, e.g. interacting with a REST API?

Looking at sokol_fetch.h it looks like receiving only, not uploading (a related issue discussing sokol_fetch.h and bidirectional comms with websockets: https://github.com/floooh/sokol/issues/367)

Looking for possible solutions:

The Emscripten Fetch API allows native code to transfer files via XHR (HTTP GET, PUT, POST) and Emscripten_fetch can be used to upload files to remote servers via HTTP PUT https://emscripten.org/docs/api_reference/fetch.html

Ways to interact with javascript with emscripten: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-from-c-c

Similar discussion but for "Making a website with Godot", with a good comment: https://www.reddit.com/r/godot/comments/ga158t/comment/foxl2b2/?utm_source=reddit&utm_medium=web2x&context=3

It's cool that e.g. this nuklear demo is only a 350kB wasm file https://floooh.github.io/sokol-html5/nuklear-sapp.html versus minimal godot HTML5 exports look like ~4MB

floooh commented 1 year ago

The sokol headers are not a general SDK for building complete applications, they might just provide a couple of cross-platform building blocks.

If the goal is a web application (as opposed to a cross-platform application that also runs on the web), I would probably use a hybrid approach, where big blocks of functionality are implemented in JS (for instance things like communicating with REST endpoints), and the WASM code calls out into JS (or the opposite approach that the 'main loop' is implemented in JS and calls into WASM functions.

digitalsignalperson commented 1 year ago

I also noticed from https://github.com/floooh/sokol/issues/342

sokol_fetch.h: CURL for HTTP(S) requests on non-web platforms

https://github.com/bqqbarbhg/spear/blob/6e5017491fe3d9c4ac667a3d96585ccd55f7bc8a/src/ext/sokol/sokol_fetch_impl.h#L1673-L2001

Thoughts on implementing more curl in a cross platform way? For wasm: https://github.com/wapm-packages/curl

floooh commented 1 year ago

I did something similar in an older project: https://github.com/floooh/oryol/tree/master/code/Modules/HttpFS

This attempts to use platform-native APIs for talking to HTTP endpoints. There's a couple of issues with this approach (mainly that the Windows HTTP API sucks), and in the end it's almost certainly better to link with curl on native platforms and use the fetch API on the web. Integrating curl into the build system on native platforms isn't trivial either though, so a sokol header which requires libcurl as dependency would kinda defeat the purpose of STB style headers (which always should be easy to integrate).

TL;DR: there most likely won't be a sokol header which provides a generic HTTP wrapper API.