td: Wasm: failed to open connection

I am trying to implement TDLib client for NodeJS with WebAssembly. Everything works fine before I send setAuthenticationPhoneNumber request. Then I receive an error: Failed to open connection.

Can you advise something?

Settings:

tdlibParameters {
    use_test_dc = true
    database_directory = "./dev/db"
    files_directory = ""
    use_file_database = false
    use_chat_info_database = false
    use_message_database = false
    use_secret_chats = false
    api_id = xxx
    api_hash = "xxx"
    system_language_code = "en"
    device_model = "UNKNOWN DEVICE"
    system_version = "UNKNOWN VERSION"
    application_version = "0.1.0"
    enable_storage_optimizer = false
    ignore_file_names = false
  }

Log:

[ 3][t 0][1562392992.641999960][Td.cpp:3285][!Td][&td_requests] Receive request 5: setAuthenticationPhoneNumber {
  phone_number = "xxx"
  allow_flash_call = false
  is_current_phone_number = false
}

[ 4][t 0][1562392992.641999960][utils.cpp:21][!AuthManager]     Create storer for auth_sendCode {
  flags = 0
  phone_number = "xxx"
  api_id = xxx
  api_hash = "xxx"
}

[ 3][t 0][1562392992.641999960][NetQuery.h:347][!AuthManager]   [Query:[id:196608][tl:0x86aef0ec][state:Query]]

[ 3][t 0][1562392992.641999960][NetQuery.h:239][!AuthManager][&net_query]       [Query:[id:196608][tl:0x86aef0ec][state:Query]] [debug:dispatch]

[ 3][t 0][1562392992.641999960][NetQuery.h:239][!AuthManager][&net_query]       [Query:[id:196608][tl:0x86aef0ec][state:Query]] [debug:sent to main session multi proxy DcId{2}]

[ 3][t 0][1562392992.641999960][NetQuery.h:239][!SessionMultiProxy:2:main][&net_query]  [Query:[id:196608][tl:0x86aef0ec][state:Query]] [debug:SessionMultiProxy:2:main: send to proxy #0]

[ 3][t 0][1562392992.643000126][NetQuery.h:239][!SessionProxy:2:main][&net_query]       [Query:[id:196608][tl:0x86aef0ec][state:Query]] [debug:SessionProxy:2:main: sent to session]

[ 3][t 0][1562392992.643000126][NetQuery.h:239][!Session:2:main][&net_query]    [Query:[id:196608][tl:0x86aef0ec][state:Query]] [debug:Session: received from SessionProxy]

[ 3][t 0][1562392992.643000126][Session.cpp:230][!Session:2:main][&net_query]   Got query [Query:[id:196608][tl:0x86aef0ec][state:Query]]

[ 3][t 0][1562392992.643000126][NetQuery.h:239][!Session:2:main][&net_query]    [Query:[id:196608][tl:0x86aef0ec][state:Query]] [debug:Session: pending]

[ 3][t 0][1562392993.563000202][StateManager.cpp:139][!State manager]   Auto set net_type = Other

[ 3][t 0][1562392993.563000202][ConnectionCreator.cpp:754][!ConnectionCreator][&connections]    Receive network flag true with generation 1

[ 3][t 0][1562392993.564000130][ConnectionCreator.cpp:759][!ConnectionCreator][&connections]    Set proxy query token to 0: 1 1

[ 3][t 0][1562392993.564000130][ConnectionCreator.cpp:925][!ConnectionCreator][&connections]    In client_loop: [client:0x5e69d1ad]

[ 3][t 0][1562392993.564000130][ConfigManager.cpp:559][!Recoverer][&config_recoverer]   Failed to connect for 0.998419

[ 3][t 0][1562392993.564000130][ConfigManager.cpp:619][!Recoverer][&config_recoverer]   Wakeup in 4001.6ms

[ 2][t 0][1562392993.932000160][Session.cpp:1121][!Session:2:main::HandshakeActor]      Failed to open connection: [Error : 0 : Connection closed : [172.29.2.0:443] to DcId{2} from [0.0.0.0:0]]

Full log: wasm.log

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I opened an issue in the emscripten repository: https://github.com/emscripten-core/emscripten/issues/12813

I managed to get it to work on Node.js.

  • browser:
good
  • node:
bad

The issue is that ws doesn’t send the Sec-WebSocket-Protocol header.

I looked into the code the code that emscripten has generated in the td_wasm.js file:

opts = ENVIRONMENT_IS_NODE ? {
  "protocol": subProtocols.toString(),
} : subProtocols
// ...
  WebSocketConstructor = require("ws")
// ...
ws = new WebSocketConstructor(url, opts);
ws.binaryType = "arraybuffer"

ws doesn’t support the ‘protocol’ option anymore: docs. It should be passed in the second argument.

So, Emscripten is not intended to work with the latest ws. It works correctly with ws@2 (which was released in 2017).

I also got it to work with these patches on the latest ws 7.4.0:

-subProtocols = subProtocols.replace(/^ +| +$/g, "").split(/ *, */);
-opts = ENVIRONMENT_IS_NODE ? {
-  "protocol": subProtocols.toString(),
-} : subProtocols
+subProtocols = subProtocols.replace(/^ +| +$/g, "").split(/ *, */);
+opts = subProtocols
-peer.socket.on("message", function(data, flags) {
-  if (!flags.binary) {
-    return
-  }
-  handleMessage(new Uint8Array(data).buffer)
-});
+peer.socket.on("message", function(data) { // (the flags argument doesn't exist anymore)
+  handleMessage(new Uint8Array(data).buffer)
+});

It seems to work, but note that something might be broken.


Also, WebAssembly should make it much easier to run TDLib in Node.js. Since Node.js exports OpenSSL symbols, TDLib (at least dynamically linked) uses the symbols from the node.js binary and not from the system. This requires either building TDLib with the exact same openssl version that the pre-built Node.js is linked against, or re-building Node.js with linking against the system openssl. I’ve also had random segfaults after some time running, even when only the letter in the openssl versions differs, so no more different openssl versions for me.