v3/examples/websocket-transport/README.md
This example demonstrates how to use a custom transport like WebSocket for Wails IPC instead of the default HTTP fetch transport. All Wails bindings and features work identically - only the underlying transport layer changes.
NewWebSocketTransport()setTransport() and createWebSocketTransport()┌─────────────────────────────────────────┐
│ Frontend (JavaScript) │
│ - setTransport(wsTransport) │
│ - All bindings work unchanged │
└───────────────┬─────────────────────────┘
│
│ WebSocket (ws://localhost:9099)
│
┌───────────────▼─────────────────────────┐
│ Backend (Go) │
│ - WebSocketTransport on port 9099 │
│ - Standard MessageProcessor │
│ - All Wails features available │
└─────────────────────────────────────────┘
Navigate to this directory:
cd v3/examples/websocket-transport
Run the example:
go run .
The application will start with:
ws://localhost:9099/wails/wsThe backend configuration is simple - just pass a WebSocketTransport to the application options:
// Create WebSocket transport on port 9099
wsTransport := NewWebSocketTransport(":9099")
app := application.New(application.Options{
Name: "WebSocket Transport Example",
Services: []application.Service{
application.NewService(&GreetService{}),
},
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
// Use WebSocket transport instead of default HTTP
Transport: wsTransport,
})
The frontend uses the WebSocket transport with generated bindings:
import { setTransport } from "/wails/runtime.js";
import { createWebSocketTransport } from "/websocket-transport.js";
import { GreetService } from "/bindings/github.com/wailsapp/wails/v3/examples/websocket-transport/index.js";
// Create and configure WebSocket transport
const wsTransport = createWebSocketTransport('ws://localhost:9099/wails/ws', {
reconnectDelay: 2000, // Reconnect after 2 seconds if disconnected
requestTimeout: 30000 // Request timeout of 30 seconds
});
// Set as the active transport
setTransport(wsTransport);
// Now all generated bindings use WebSocket instead of HTTP fetch!
const result = await GreetService.Greet("World");
Key Point: The generated bindings (GreetService.Greet(), GreetService.Echo(), etc.) automatically use whatever transport is configured via setTransport(). This proves the custom transport hijack works seamlessly with Wails code generation!
All generated bindings work identically with WebSocket transport:
GreetService.Greet(name) - Simple string parameter and returnGreetService.Echo(message) - Echo back messagesGreetService.Add(a, b) - Multiple parameters with numeric typesGreetService.GetTime() - No parameters, string return