• Stars
    star
    315
  • Rank 128,515 (Top 3 %)
  • Language
    Go
  • Created about 11 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

HTML5 Server Sent Events with Go

Golang HTML5 SSE Example

This is an minimalistic example of how to do HTML5 Server Sent Events with Go (golang). From the server's perspective, SSE is nearly identical to long polling. The client makes a GET request that establishes a TCP connection. The server keeps this connection open and sends events to the client when they are available. In this example, the server pushes a new event every five seconds, consisting of a short message with the current time. Any number of clients can be connected: they will all receive the same events if they're connected concurrently. (This is achieved using Go's channels and a fan-out pattern. In other languages you may need to use some kind of pubsub messaging, like Redis or Zeromq.)

The main advantage of HTML5 SSE over long polling is that there is a nice API for it in modern browsers, so that you need not use iframes and such. SSE is easier than Websockets in the sense that it communicates exclusively over HTTP and therefore does not require a separate server. Websockets, however, supports two-way real-time communication between the client and the server.

Installing

Check out the repository from GitHub

git clone https://github.com/kljensen/golang-html5-sse-example

Running

To run the server, do

go run ./server.go

Then point your web browser to http://localhost:8000. You should see output in the browser like the following:

Yo Duder, here are some facinating messages about the current time:
Message: 20 - the time is 2013-03-08 21:08:01.260967 -0500 EST
Message: 21 - the time is 2013-03-08 21:08:06.262034 -0500 EST
Message: 22 - the time is 2013-03-08 21:08:11.262608 -0500 EST
Message: 23 - the time is 2013-03-08 21:08:16.263491 -0500 EST
Message: 24 - the time is 2013-03-08 21:08:21.264218 -0500 EST
Message: 25 - the time is 2013-03-08 21:08:26.264433 -0500 EST

And, you should see output in the terminal like the following

2013/03/15 03:55:55 Sent message 0 
2013/03/15 03:55:55 Broadcast message to 0 clients
2013/03/15 03:55:55 Added new client
2013/03/15 03:55:55 Added new client
2013/03/15 03:55:58 Finished HTTP request at  /
2013/03/15 03:55:58 Added new client
2013/03/15 03:56:00 Sent message 1 
2013/03/15 03:56:00 Broadcast message to 3 clients
2013/03/15 03:56:05 Sent message 2 
2013/03/15 03:56:05 Broadcast message to 3 clients
2013/03/15 03:56:10 Sent message 3 
2013/03/15 03:56:10 Broadcast message to 3 clients
2013/03/15 03:56:15 Sent message 4 
2013/03/15 03:56:15 Broadcast message to 3 clients
2013/03/15 03:56:20 Sent message 5 
2013/03/15 03:56:20 Broadcast message to 3 clients

Thanks

This code is based off of a few sources, mostly

License (the Unlicense)

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

More Repositories

1

async-flask-sqlalchemy-example

Non-blocking Flask, SQLAlchemy, Psygopg2, Postgresql, Gevent & Gunicorn Example
Python
265
star
2

snowball

Go implementation of the Snowball stemmers
Go
244
star
3

mit-phd-thesis

MIT Ph.D. Thesis in LaTeX
PostScript
62
star
4

node-sse-example

JavaScript
31
star
5

golang-chat

A minimal Go TCP chat example
Go
30
star
6

go-is-awesome

Presentation about Golang
HTML
19
star
7

Gemoda

A generic motif discovery algorithm for sequential data
C
9
star
8

py-reqcache

Caching for Python's Request Package
Python
7
star
9

shell-interval-training-timer

shell-interval-workout-timer
Shell
6
star
10

semiuniq

A uniq-like tool for removing nearby repeated lines in a file"
Rust
4
star
11

viff

Fork of http://viff.dk/
Python
4
star
12

biogrep

A multiโ€“threaded pattern matcher for large pattern sets
C
3
star
13

ctbiomed

Analyzes biomedical research trends in Connecticut
Python
2
star
14

example-attackable-web-app

A web app vulnerable to SQL injection, XSS, and CSRF.
Python
2
star
15

yale-class-chat

Chat, Q&A, voting, and attendance-taking platform for use in Yale classes
Elixir
2
star
16

docker-actix-postgres-redis-todo

A version of the Actix todo example, dockerized with redis for session storage
Rust
2
star
17

meteor-coursera

Coursera OAuth flow for Meteor
JavaScript
1
star
18

hollablog

Blog for Kyle and Gilman. Woot.
1
star
19

homebrew-tap

Homebrew tap for code from Kyle Jensen (kljensen)
Ruby
1
star
20

binomp

P-value for binomial distribution
C
1
star
21

cleanup-heroku

A portable, POSIX-compliant shell script to delete old, unused Heroku applications
Shell
1
star
22

basement-motion-lights

Code for my motion-activated basement lighting
C++
1
star
23

borkedbuild

Make custom build badges for you software projects
Go
1
star
24

pgtap-results-approx-eq

Compare the results of two PostgreSQL queries to determine if they are "approximately" the same
PLpgSQL
1
star
25

j2tex

Code I use to create templated LaTeX documents
Python
1
star