http server in tauri app #2751
-
Hi all, |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 16 replies
-
You have to bundle a server yourself, either in rust or via a sidecar ( docs / example ). |
Beta Was this translation helpful? Give feedback.
-
Hello how I implement Rocket inside tauri |
Beta Was this translation helpful? Give feedback.
-
I also have a similar QA: #6731 |
Beta Was this translation helpful? Give feedback.
-
Hi I know this is an old thread, but I am able to run a web server through Actix Web but I want to trigger an event to the frontend when I get that request. Is this possible? I am new to Rust and can't figure out how to get a proper reference to the app handle. let app_handler = app.handle();
tauri::async_runtime::spawn(
HttpServer::new(move || {
App::new().route(
"/",
web::get().to(|| async {
app_handler.emit(
"hello",
Some(Payload {
message: "Hello from Rust!".to_string(),
}),
);
HttpResponse::Ok().body("hello world!")
}),
)
})
.bind(("127.0.0.1", 8080))?
.run(),
); |
Beta Was this translation helpful? Give feedback.
-
@snilan @mandaputtra - any update? Are you using Tauri 1.x or 2.x beta? Or, are there better ways to enable customers to send commands to my app and get back a response? |
Beta Was this translation helpful? Give feedback.
-
I have been wonder this too |
Beta Was this translation helpful? Give feedback.
-
Creating a HTTP Server in a Tauri app is useful to share functionality with smartphones in your local network. Or is there any other way to call a Tauri command on a phone when the Tauri app is running on a computer in the same network? |
Beta Was this translation helpful? Give feedback.
-
I ended up doing something like this (in case anyone wants to do the same) to start a web server and share the Tauri App Handle with Actix-web requests: tauri::Builder::default()
.setup(|app| {
// Only start the server if we are in a desktop environment
#[cfg(desktop)]
{
let app_handle = app.handle().clone();
thread::spawn(|| {
server::init(app_handle).unwrap();
});
}
Ok(())
}) struct AppState {
tauri: AppHandle,
}
#[actix_web::main]
pub async fn init(tauri: AppHandle) -> io::Result<()> {
let app_data = web::Data::new(AppState { tauri });
let http_server = HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
.allow_any_method()
.allow_any_header()
.max_age(3600);
App::new()
.app_data(app_data.clone())
.wrap(cors)
.wrap(middleware::Logger::default())
.service(handlers::system::state)
})
.bind(("0.0.0.0", 1111))
.expect("failed to bind address")
.run();
println!("Server started at http://0.0.0.0:1111");
http_server.await
} #[post("/system/{action}")]
async fn state(
(action, payload, data): (Path<String>, Json<StatePayload>, Data<AppState>),
) -> HttpResponse {
let shell = data.tauri.shell();
...
} |
Beta Was this translation helpful? Give feedback.
You have to bundle a server yourself, either in rust or via a sidecar ( docs / example ).
Or if you already know a crazy way to host a server inside of a browser you probably can make that work too 🤷 (at least if i understood your usecase correctly)