emscripten-core / emscripten-core/emscripten
websocket callbacks occur on main thread despite socket being created on a separate thread
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Hey emscripten team, thanks for the work on this awesome project. I'll be brief explaining my question.
In my program I create a separate thread, and in that thread I create a websocket and setup the onopen, onmessage, etc callback functions. Then that separate thread calls `emscripten_set_main_loop(myloop, 5, false);` to periodically do some work.
I'm observing something that I don't understand... it looks like the socket callback functions are being executed on the main thread, not on thread that created the socket and registered the callbacks. Here's a minimal example:
```
#include
#include
#include
#include
void gameLoop() {}
void socketLoop() {}
...
EM_BOOL onmessage(int eventType, const EmscriptenWebSocketMessageEvent *websocketEvent, void *userData) {
puts("onmessage");
std::cout << "ON MESSAGE! " << std::this_thread::get_id() << "\n";
return EM_TRUE;
}
int main() {
new std::thread([]() {
std::cout << "Initializing socket connection " << std::this_thread::get_id() << "\n";
// Docs: https://github.com/emscripten-core/emscripten/blob/main/system/include/emscripten/websocket.h
EmscriptenWebSocketCreateAttributes ws_attrs = {
"ws://localhost:3000",
NULL,
false
};
mWebSocket = emscripten_websocket_new(&ws_attrs);
emscripten_websocket_set_onopen_callback(mWebSocket, NULL, onopen);
emscripten_websocket_set_onerror_callback(mWebSocket, NULL, onerror);
emscripten_websocket_set_onclose_callback(mWebSocket, NULL, onclose);
emscripten_websocket_set_onmessage_callback(mWebSocket, NULL, onmessage);
// Loop where I periodically do some work
emscripten_set_main_loop(socketLoop, 5, false);
});
// Main thread loop where I do some rendering
emscripten_set_main_loop(gameLoop, 0, true);
}
```
When I run that I see something like the following printed:
```
Hello from main() 16180
Initializing socket connection 5266408
... Later when the server send a message to the client
ON MESSAGE! 16180
```
Why is the socket onmessage callback handler executed on the main thread? Am I thinking about this the wrong way and that's the only thread on which these callbacks can be executed?
The EmscriptenWebSocketCreateAttributes has an attribute called `createOnMainThread` which I'm setting to false based on what the documentation says. However, this attribute doesn't seem to have any effect.
Thanks a ton for the help!
---
Please include the following in your bug report:
**Version of emscripten/emsdk: 3.1.2**
Please include the output `emcc -v` here
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.2 (a77b559a8b40b7e89fc8c17e41034128df9543e4)
clang version 14.0.0 (https://github.com/llvm/llvm-project 782c0dd1a1c235afb09a34e7da4a1267ead14765)
Target: wasm32-unknown-emscripten
Thread model: posix
Contributor guide
Assessment
This issue has not been assessed yet.