# README
Autoreload Package Documentation
The autoreload
package provides a simple WebSocket-based solution for automatically reloading web pages in Go web applications. It offers real-time communication between the server and client, allowing developers to trigger page reloads or send custom messages.
Features
- WebSocket server for real-time communication
- Customizable JavaScript client for easy integration
- Broadcast functionality to trigger reloads or send custom messages
Installation
To use the autoreload
package in your project, add it to your Go module:
go get github.com/go-go-golems/clay/pkg/autoreload
Usage
Here's a short tutorial on how to use the autoreload
package in your Go web application:
- Import the package:
import "github.com/go-go-golems/clay/pkg/autoreload"
- Create a new WebSocket server instance:
wsServer := autoreload.NewWebSocketServer()
- Set up the WebSocket handler:
http.HandleFunc("/ws", wsServer.WebSocketHandler())
- Serve the JavaScript snippet:
http.HandleFunc("/autoreload.js", func(w http.ResponseWriter, r *http.Request) {
js := wsServer.GetJavaScript("/ws")
w.Header().Set("Content-Type", "application/javascript")
w.Write([]byte(js))
})
- Include the JavaScript in your HTML:
<script src="/autoreload.js"></script>
- Trigger reloads or send custom messages:
wsServer.Broadcast("reload")
Example
Here's a complete example demonstrating how to use the autoreload
package:
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/go-go-golems/clay/pkg/autoreload"
)
func main() {
// Create a new WebSocket server instance
wsServer := autoreload.NewWebSocketServer()
// Set up the WebSocket handler
http.HandleFunc("/ws", wsServer.WebSocketHandler())
// Serve the JavaScript snippet at a specific endpoint
http.HandleFunc("/autoreload.js", func(w http.ResponseWriter, r *http.Request) {
js := wsServer.GetJavaScript("/ws")
w.Header().Set("Content-Type", "application/javascript")
w.Write([]byte(js))
})
// Serve a simple HTML page
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
html := `
<!DOCTYPE html>
<html>
<head>
<title>Autoreload Demo</title>
<script src="/autoreload.js"></script>
</head>
<body>
<h1>Autoreload Demo</h1>
<p>This page will reload automatically every 5 seconds.</p>
<p>Current time: ` + time.Now().Format(time.RFC3339) + `</p>
</body>
</html>
`
fmt.Fprint(w, html)
})
// Trigger a reload every 5 seconds
go func() {
for {
time.Sleep(5 * time.Second)
wsServer.Broadcast("reload")
}
}()
// Start the HTTP server
log.Println("Server started on http://localhost:8080")
This example sets up a simple web server that:
- Creates a WebSocket server for autoreload functionality
- Serves the autoreload JavaScript at
/autoreload.js
- Serves a simple HTML page at the root URL
- Triggers a reload every 5 seconds using a goroutine
To run this example:
- Save the code in a file named
main.go
- Run the following command:
go run main.go
- Open a web browser and navigate to
http://localhost:8080
You should see a page that automatically reloads every 5 seconds, updating the current time displayed.
Customization
The autoreload
package allows for customization of the WebSocket endpoint and behavior. You can modify the WebSocket URL when getting the JavaScript snippet:
js := wsServer.GetJavaScript("/custom-ws-endpoint")
You can also send custom messages instead of just "reload":
wsServer.Broadcast("custom-message")
To handle these custom messages, you'll need to modify the JavaScript code accordingly.