package
1.0.0-beta.2
Repository: https://github.com/starfederation/datastar.git
Documentation: pkg.go.dev
# README
Go SDK for Datastar
Implements the SDK spec and exposes an abstract ServerSentEventGenerator struct that can be used to implement runtime specific classes.
Usage is straightforward:
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"log/slog"
"net/http"
"os"
"time"
datastar "github.com/starfederation/datastar/sdk/go"
)
const port = 9001
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
mux := http.NewServeMux()
cdn := "https://cdn.jsdelivr.net/gh/starfederation/datastar@develop/bundles/datastar.js"
style := "display:flex;flex-direction:column;background-color:oklch(25.3267% 0.015896 252.417568);height:100vh;justify-content:center;align-items:center;font-family:ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';"
page := []byte(fmt.Sprintf(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
<script type="module" defer src="%s"></script>
</head>
<body style="%s">
<span id="feed" data-on-load="%s"></span>
</body>
</html>
`, cdn, style, datastar.GetSSE("/stream")))
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
w.Write(page)
})
mux.HandleFunc("GET /stream", func(w http.ResponseWriter, r *http.Request) {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
sse := datastar.NewSSE(w, r)
for {
select {
case <-r.Context().Done():
logger.Debug("Client connection closed")
return
case <-ticker.C:
bytes := make([]byte, 3)
_, err := rand.Read(bytes)
if err != nil {
logger.Error("Error generating random bytes: ", slog.String("error", err.Error()))
return
}
hexString := hex.EncodeToString(bytes)
frag := fmt.Sprintf(`<span id="feed" style="color:#%s;border:1px solid #%s;border-radius:0.25rem;padding:1rem;">%s</span>`, hexString, hexString, hexString)
sse.MergeFragments(frag)
}
}
})
logger.Info(fmt.Sprintf("Server starting at 0.0.0.0:%d", port))
if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), mux); err != nil {
logger.Error("Error starting server:", slog.String("error", err.Error()))
}
}
Examples
The Datastar website acts as a set of examples for how to use the SDK.
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
The default attributes for <script/> element use when executing scripts.
Default value for FragmentMergeMode Morphs the fragment into the existing element using idiomorph.
The default duration for settling during fragment merges.
The default duration for retrying SSE on connection reset.
No description provided by the author
An event for executing <script/> elements in the browser.
An event for merging HTML fragments into the DOM.
An event for merging signals.
An event for removing HTML fragments from the DOM.
An event for removing signals.
Inserts the fragment after the existing element.
Appends the fragment to the existing element.
Inserts the fragment before the existing element.
Replaces the inner HTML of the existing element.
Morphs the fragment into the existing element using idiomorph.
Replaces the outer HTML of the existing element.
Prepends the fragment to the existing element.
Upserts the attributes of the existing element.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
region Dataline literals.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
Should script element remove itself after execution?.
Should fragments be merged using the ViewTransition API?.
Should a given set of signals merge if they are missing?.
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author
region The type protocol on top of SSE which allows for core pushed based communication between the server and the client.
No description provided by the author
region The mode in which a fragment is merged into the DOM.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author