one
one
one
one
chat body reloaded
`)) }) go func() { i := 0 for { i time.Sleep(1 * time.Second) server.TryPublish(\\\"messages\\\", &sse.Event{ ID: []byte(fmt.Sprintf(\\\"%d\\\", i)), Event: []byte(\\\"post-1-changed\\\"), Data: []byte(`
Hello from sse ` fmt.Sprintf(\\\"%d\\\", i) `
`), }) server.TryPublish(\\\"messages\\\", &sse.Event{ ID: []byte(fmt.Sprintf(\\\"%d\\\", i)), Event: []byte(\\\"notifications\\\"), Data: []byte(`
Hello from post 2 sse ` fmt.Sprintf(\\\"%d\\\", i) `
`), }) server.TryPublish(\\\"messages\\\", &sse.Event{ ID: []byte(fmt.Sprintf(\\\"%d\\\", i)), Event: []byte(\\\"chatter\\\"), Data: []byte(`
`), }) server.EventTTL = 5 * time.Second } }() http.ListenAndServe(\\\":9999\\\", mux)} ","image":"http://www.luping.net/uploads/20240903/172533420366d682bb82a48.jpg","datePublished":"2024-11-07T21:21:13+08:00","dateModified":"2024-11-07T21:21:13+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > go htmx and sse example

go htmx and sse example

Published on 2024-11-07
Browse:543

go htmx and sse example

This example demonstrates how to replace few blocks on event for example "post with id 1 changed" ( post-1-changed ) and trigger to load content via ajax request on "chatter" event.

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/r3labs/sse/v2"
)

func main() {
    server := sse.New()
    _ = server.CreateStream("messages")

    mux := http.NewServeMux()
    mux.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {

        // la security
        token := r.URL.Query().Get("token")
        if token != "secret" {
            http.Error(w, "invalid token", http.StatusUnauthorized)
            return
        }
        go func() {
            Hello from chat room`))
    })

    mux.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte(`
        
        
        
one
one
one
one
chat body reloaded
`)) }) go func() { i := 0 for { i time.Sleep(1 * time.Second) server.TryPublish("messages", &sse.Event{ ID: []byte(fmt.Sprintf("%d", i)), Event: []byte("post-1-changed"), Data: []byte(`
Hello from sse ` fmt.Sprintf("%d", i) `
`), }) server.TryPublish("messages", &sse.Event{ ID: []byte(fmt.Sprintf("%d", i)), Event: []byte("notifications"), Data: []byte(`
Hello from post 2 sse ` fmt.Sprintf("%d", i) `
`), }) server.TryPublish("messages", &sse.Event{ ID: []byte(fmt.Sprintf("%d", i)), Event: []byte("chatter"), Data: []byte(`
`), }) server.EventTTL = 5 * time.Second } }() http.ListenAndServe(":9999", mux) }
Release Statement This article is reproduced at: https://dev.to/blinkinglight/go-htmx-and-sse-example-55k6?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3