Frontend/Backend Communication
Frontend/backend communication
How do the micro-app frontend UI and the Go backend talk to each other? One minute is enough to grasp the core mechanism.
Minimal communication model
Frontend UI (Vue / React / HTML)
│
│ window.$gm.request('方法名', { 参数 })
▼
GM Desktop client (relayed by the main process)
│
│ Unix Socket (tmp/app.sock)
▼
Backend service (Go daemon)- Frontend: no need to care about ports or IPs; just call
window.$gm.request('方法名', { 参数 }). - GM Desktop: works as the secure bridge and passes the request to your Go program over the local file socket.
- Go backend: receives the request, handles it, and returns the result to the frontend along the same path.
Developers only need to do two things
1. Backend: write a handler and register the method name
Register the methods you want to expose in internal/handler/rpc/server.go:
// Register a method named "getHello"
s.RegisterHandle("getHello", func(req *gsock.Request) (any, error) {
var params struct {
Name string `json:"name"`
}
rpcutil.ParseParams(req, ¶ms)
return "你好," + params.Name, nil
})Built-in heartbeat check
The template registers a ping method by default. It is the credential GM Desktop uses to check every 5 seconds whether the backend is alive, so keep it and do not delete it.
2. Frontend: make the call and get the result
In the frontend page, call it directly through the global $gm object:
// The frontend calls the "getHello" method you just registered
const res = await window.$gm.request('getHello', { name: '开发者' });
console.log(res); // Output: "你好,开发者"Why are there no ports and IPs to configure?
- No network ports used: communication goes through a local server file (
tmp/app.sock), so no TCP/HTTP port is occupied at all, and it never conflicts with Nginx, MySQL, or other services on the server. - No configuration: the template adapts the paths for both development and production environments, so you only focus on writing business methods.
- Secure by nature: the outside network cannot scan or reach this socket; only the GM Desktop main process can communicate over it.
