Socket.io is a library/framework which enables bidirectional communication between browser and server. The goal in mind is to update the browser status from the server side. I'll go through a simple solution I made. All the code is not here, you have to see it in github, see links at the end of the post.
Go-backend
Backend server serves all static assets, rendered html and also the server side socket.io traffic.
Template
First the Go backend is done with basic html template with speed in mind. We’re showing simple RSS data with minor tweeks. Nothing special here.
<body> <div id="news-container"> {{ range .news }} <div class="item"> <div class="date">{{ .PubDate.Local.Format "02.01. 15:04" }}</div> <div class="source">{{ .RssSource }}</div> <div class="category">{{ .Category.CategoryName }}</div> <div class="link"> <a target="_blank" id="{{ .Id.Hex }}" href="{{ .RssLink }}">{{ .RssTitle }}</a> </div> </div> {{end}} </div> <script src="/public/js/socket.io.js" type="application/javascript"></script> <script src="/public/js/moment.min.js" type="application/javascript"></script> <script src="/public/js/uutispuro.js" type="application/javascript"></script> </body>
Handling socket.io traffic
We're using a ready library which supports the latest socket.io version.
import ”github.com/googollee/go-socket.io"
Initialize server
server, err := socketio.NewServer(nil) if err != nil { log.Fatal(err) }
Listen for browsers to connect.
server.On("connection", func(so socketio.Socket) { so.Join("news") for _ = range time.Tick(10 * time.Second) { // get RSS titles from db news, _ := json.Marshal(app.Sessions.FetchRssItems("fi")) // broadcast so.BroadcastTo("news", "message", string(news)) } so.On("disconnection", func() { log.Println("on disconnect") }) }) server.On("error", func(so socketio.Socket, err error) { log.Println("error:", err) })
log.Fatal(http.ListenAndServe(":1300", nil))
Frontend: javascript for handling socket.io traffic
When we get a message from the server, we parse the json, make a dom node out of it and prepend the first five titles.
window.onload = function() { var socket = io(); socket.on('message', function(msg) { var json = JSON.parse(msg); for (var i = 4; i >= 0; i--) { if (document.getElementById(json.news[i].id) === null) { var item = makeNode(json, i); prepend(item); } } json, item = null; }); } var prepend = function(firstElement) { var parent = document.getElementById('news-container'); parent.insertBefore(firstElement, parent.firstChild); parent.removeChild(parent.lastChild) }
Socket.io library for golang - https://github.com/googollee/go-socket.io
Socket.io - http://socket.io
Socket.io - http://socket.io
Go html templates - https://golang.org/pkg/html/template/