feat: add embedded NATS pub/sub support on Context

Define PubSub and Subscription interfaces in the core via package with
a vianats sub-package providing the embedded NATS + JetStream
implementation. Expose c.Publish() and c.Subscribe() on Context with
automatic subscription cleanup on session close. Refactor the NATS
chatroom example to use the built-in methods instead of manual
subscription tracking.
This commit is contained in:
Ryan Hamamura
2026-01-26 08:06:50 -10:00
parent 88bd0f31df
commit 30cc6d88e6
7 changed files with 374 additions and 153 deletions

5
via.go
View File

@@ -40,6 +40,7 @@ type V struct {
documentFootIncludes []h.H
devModePageInitFnMap map[string]func(*Context)
sessionManager *scs.SessionManager
pubsub PubSub
datastarPath string
datastarContent []byte
datastarOnce sync.Once
@@ -117,6 +118,9 @@ func (v *V) Config(cfg Options) {
if cfg.DatastarPath != "" {
v.datastarPath = cfg.DatastarPath
}
if cfg.PubSub != nil {
v.pubsub = cfg.PubSub
}
}
// AppendToHead appends the given h.H nodes to the head of the base HTML document.
@@ -525,6 +529,7 @@ func New() *V {
v.logErr(c, "failed to handle session close: %v", err)
return
}
c.unsubscribeAll()
c.stopAllRoutines()
v.logDebug(c, "session close event triggered")
if v.cfg.DevMode {