#Cannot get tauri-ivoke-http to work

11 messages · Page 1 of 1 (latest)

halcyon forge
#

Hey, I was wondering if someone would be willing to give me a hand getting tauri-invoke-http to work.

Intended use case: I am creating a desktop application using Tauri. However, I want my user (who has launched the desktop application) to be able to occasionally load the app on some other device on the local network and be able to invoke Rust from the local device browser to the running Tauri desktop application.

From what I understand, this can be achieved using the localhost plugin. However, the Tauri API is only available in Tauri windows and not on the browser of the local device. To be able to invoke Rust from a non-tauri window, this must be done over HTTP. Which, again, from what I understand, can be achieved using tauri-invoke-http.

I have been unable to invoke over HTTP successfully.

I have changed my main.rs according to the tauri-invoke-http readme.
I set withGlobalTauri in the conf to true.
I am invoking using window.__TAURI__invoke

But on the browser, I get the error: "TypeError: window.TAURI is undefined".

Can anyone help me get tauri-invoke-http up and running? Thanks

#

Here is my cargo.toml

[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
edition = "2021"
rust-version = "1.57"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.4.0", features = [] }

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.4.0", features = ["api-all"] }
mac_address = "1.1.4"
tauri-plugin-localhost = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
portpicker = "0.1" 
tauri-invoke-http = { git = "https://github.com/tauri-apps/tauri-invoke-http", branch = "dev" }

[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = ["custom-protocol"]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = ["tauri/custom-protocol"]
#

Here is my tauri.conf.json

{
  "$schema": "../node_modules/@tauri-apps/cli/schema.json",
  "build": {
    "beforeBuildCommand": "npm run build",
    "beforeDevCommand": "npm run start",
    "devPath": "http://localhost:3000",
    "distDir": "../build",
    "withGlobalTauri": true
  },
  "package": {
    "productName": "nameofmyapp",
    "version": "0.1.0"
  },
  "tauri": {
    "allowlist": {
      "all": true
    },
    "bundle": {
      "active": true,
      "category": "DeveloperTool",
      "copyright": "",
      "deb": {
        "depends": []
      },
      "externalBin": [],
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/[email protected]",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "identifier": "com.nameofmyapp.dev",
      "longDescription": "",
      "macOS": {
        "entitlements": null,
        "exceptionDomain": "",
        "frameworks": [],
        "providerShortName": null,
        "signingIdentity": null
      },
      "resources": [],
      "shortDescription": "",
      "targets": "all",
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    },
    "security": {
      "csp": null
    },
    "updater": {
      "active": false
    },
    "windows": []
  }
}
#

Here is my main function

fn main() {
  let port = portpicker::pick_unused_port().expect("failed to find unused port");
  let mut context = tauri::generate_context!();
  let url = format!("http://localhost:{}", port).parse().unwrap();

  println!("{}", url);

  let window_url = WindowUrl::External(url);
  // rewrite the config so the IPC is enabled on this URL
  context.config_mut().build.dist_dir = AppUrl::Url(window_url.clone());

  let window_url_string = window_url.to_string();

  let http = tauri_invoke_http::Invoke::new(if cfg!(feature = "custom-protocol") {
    ["tauri://localhost", &window_url_string]
  } else {
    ["http://localhost:3000", &window_url_string]
  });

  tauri::Builder::default()
    .plugin(tauri_plugin_localhost::Builder::new(port).build())
    .invoke_system(http.initialization_script(), http.responder())
    .setup(move |app| {   
      http.start(app.handle());
      let main_window = tauri::WindowBuilder::new(
        app, 
        "main".to_string(),
        if cfg!(dev) { Default::default() } else { window_url }
      ).title("title").user_agent("title_user_agent").build()?;
      

      let overhead_window = tauri::WindowBuilder::new(
        app,
        "overhead",
        tauri::WindowUrl::App("resources/screens/splash.html".into())
      ).title("Overhead").position(-5279.0, -512.0).fullscreen(true).build()?;

      overhead_window.open_devtools();
      main_window.open_devtools();

      Ok(())
    })
    .invoke_handler(tauri::generate_handler![list_of_my_invoke_commands])
    .run(context)
    .expect("error while running tauri application");
}
supple arrow
#

load the app on some other device on the local network
This is not possible with tauri-invoke-http as that wouldn't be localhost, that would be LAN (Local Area Network).

halcyon forge
halcyon forge
#

If what you say is true, then it should still work on a browser on the local machine. Which it does not

supple arrow
supple arrow
halcyon forge
#

Or is there a better way of doing this?