• Stars
    star
    489
  • Rank 89,990 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 12 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Server-sent events for Go

eventsource

Build Status

eventsource provides server-sent events for net/http server.

Usage

SSE with default options

package main

import (
    "gopkg.in/antage/eventsource.v1"
    "log"
    "net/http"
    "strconv"
    "time"
)

func main() {
    es := eventsource.New(nil, nil)
    defer es.Close()
    http.Handle("/events", es)
    go func() {
        id := 1
        for {
            es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
            id++
            time.Sleep(2 * time.Second)
        }
    }()
    log.Fatal(http.ListenAndServe(":8080", nil))
}

SSE with custom options

package main

import (
    "gopkg.in/antage/eventsource.v1"
    "log"
    "net/http"
    "strconv"
    "time"
)

func main() {
    es := eventsource.New(
        &eventsource.Settings{
            Timeout: 5 * time.Second,
            CloseOnTimeout: false,
            IdleTimeout: 30 * time.Minute,
        }, nil)
    es.SendRetryMessage(3 * time.Second)
    defer es.Close()
    http.Handle("/events", es)
    go func() {
        id := 1
        for {
            es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
            id++
            time.Sleep(2 * time.Second)
        }
    }()
    log.Fatal(http.ListenAndServe(":8080", nil))
}

SSE with custom HTTP headers

package main

import (
    "gopkg.in/antage/eventsource.v1"
    "log"
    "net/http"
    "strconv"
    "time"
)

func main() {
    es := eventsource.New(
        eventsource.DefaultSettings(),
        func(req *http.Request) [][]byte {
            return [][]byte{
                []byte("X-Accel-Buffering: no"),
                []byte("Access-Control-Allow-Origin: *"),
            }
        },
    )
    defer es.Close()
    http.Handle("/events", es)
    go func() {
        id := 1
        for {
            es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
            id++
            time.Sleep(2 * time.Second)
        }
    }()
    log.Fatal(http.ListenAndServe(":8080", nil))
}

More Repositories

1

ragel-go

Ragel codegenerator for Go (see branch `golang-6`)
C++
32
star
2

cdnstats

CDNStat is a daemon collecting various statistics from nginx requests: request count, sent bytes, referer, URI path.
Go
27
star
3

formtastic-plus-bootstrap

formtastic-plus-bootstrap makes a formtastic form to look like a bootstrap form
Ruby
22
star
4

ssv2leds

SteelSeries Siberia v2 headset's LEDs control tool
Go
12
star
5

ut181a

Uni-T UT181A digital multimeter remote control
Rust
11
star
6

docker-apache2-php5

Docker image for running Apache 2.x with PHP 5.x
GDB
8
star
7

tagged-cache

ActiveSupport cache adapter with tagged dependencies
Ruby
8
star
8

merb-recaptcha

Merb plugin that provides recaptcha.net service helpers
Ruby
7
star
9

active_record_foreign_keys

Foreign keys support for ActiveRecord
Ruby
6
star
10

redis-cache

ActiveSupport cache adapter for Redis
Ruby
6
star
11

ut181a-cli

Uni-T UT181A DMM remote control command-line interface
Rust
5
star
12

sapphire-3d-ru

Two Trees Sapphire S/Pro useful links
4
star
13

netcat

netcat in Rust
Rust
3
star
14

golog

Logging library for Go
Go
3
star
15

extjob-runner

Simple external jobs runner
Go
3
star
16

docker-apache2-php7

Docker image for running Apache 2.x with PHP 7.x
GDB
2
star
17

jobmon

External job monitoring
Go
2
star
18

docker-pdns-recursor

Docker image with local dns recursor
Shell
1
star
19

dummy-cache

ActiveSupport cache adapter that does nothing
1
star
20

cp211x_uart

Rust driver for HID-to-UART CP2110/CP2114 chipset
Rust
1
star
21

vagrant-box

Packer configurations to generate Vagrant boxes
Shell
1
star
22

dpkgdb

dpkg database API for Go
Go
1
star
23

mntent

mtab/fstab parser for Go
Go
1
star