goreactive

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2024 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goreactive - reactive template variables with WebSockets

Go Reference

goreactive allows you to build web applications such as dashboards that update in real-time without writing any JavaScript. Use standard Go templates, embed them with ReactiveVar objects and the library takes care of the rest. Updates are pushed to clients over Websockets.

Install

go get github.com/antvirf/goreactive

Example

package main

import (
	"fmt"
	"log"
	"math/rand/v2"
	"net/http"
	"text/template"
	"time"

	"github.com/antvirf/goreactive"
)

var pageData PageData

type PageData struct {
	ReactiveVarsScriptBlock string
	ReactiveVars            []*goreactive.ReactiveVar
}

func serveIndex(w http.ResponseWriter, r *http.Request) {
	template.Must(template.New("index").Parse(`
  {{ .ReactiveVarsScriptBlock }}
  <h1>Goreactive example</h1>
  <ul>
  {{ range .ReactiveVars}}
    <li>
      {{ . }}
    </li>
  {{ end  }}
  </ul>
    `)).Execute(w, pageData)
}

func main() {
	// Create a reactive var
	example_one := goreactive.NewReactiveVar("28")

	// Set up data to pass to our templates
	pageData = PageData{
		ReactiveVars: []*goreactive.ReactiveVar{example_one},
		ReactiveVarsScriptBlock: goreactive.WebsocketJavascriptBlock,
	}

	// Start updating our variable in the background
	go func() {
		for {
			<-time.After(time.Duration(rand.IntN(1000)) * time.Millisecond)
			example_one.Update(fmt.Sprintf("%d", rand.IntN(1000)))
		}
	}()

	// Set up an HTTP server as we normally would
	// Add handler for template + websocket
	mux := http.NewServeMux()
	mux.HandleFunc("/", serveIndex)
	mux.HandleFunc("/reactiveVarsWebsocket", goreactive.WebsocketServerHandler)

	handler := http.Handler(mux)
	log.Fatal(http.ListenAndServe(":8080", handler))
}

Major missing features

  • Server-side
    • Authentication
    • Review 'nicer'/proper ways to handle closing the messageBroker channel etc. when the application is stopped
    • Some way to customise the printing format of variables, or allow further customisation of what is sent to the client
    • Tests + load tests (how many websocket subscriptions can we handle?)
    • Customisable/passable logger to get information out from the package
  • Client-side
    • Inform client on error/disconnect
    • Automatic attempts to redirect to client

Documentation

Index

Constants

View Source
const (
	WebsocketJavascriptBlock = `` /* 607-byte string literal not displayed */

)

This constant provides the script block for HTML pages to connect to desired websocket.

Variables

This section is empty.

Functions

func WebsocketServerHandler

func WebsocketServerHandler(w http.ResponseWriter, r *http.Request)

Types

type ReactiveVar

type ReactiveVar struct {
	Identifier string
	Value      string
	// contains filtered or unexported fields
}

ReactiveVar stores string data in a concurrency-safe way.

func NewReactiveVar

func NewReactiveVar(value string) *ReactiveVar

func (*ReactiveVar) String

func (r *ReactiveVar) String() string

String formats the ReactiveVar as a HTML span for direct usage in HTML templates.

func (*ReactiveVar) Update

func (r *ReactiveVar) Update(newValue string)

Update provides a concurrency-safe way to update the value owned by a ReactiveVar, and triggers a change notification to all subscribed clients.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址