I'm giving a short presentation on Go based on this post. I wanted a small demo to show something more than "hello world" with it, So I came up with speck. Here's a short explanation of how it works.

Server

In the app users click in a canvas to create multi-colored "explosions" which are sent to all connected clients and displayed in their canvas. The server is written in Go and uses websockets to communicate with clients. I used my turnpike library to handle the websockets. Turnpike is an implementation of a WAMP server, which is a generic subprotocol for communicating using websockets. It specifies RPC (Remote procedure call) and PubSub (Publish-subscribe pattern) functionality. With all these tools, the server was very easy to write. Here is the entire source for the server:

package main

import (  
    "code.google.com/p/go.net/websocket"
    "github.com/jcelliott/turnpike"
    "log"
    "net/http"
)

func main() {  
    flag.Parse()
    s := turnpike.NewServer()
    ws := websocket.Handler(s.HandleWebsocket)
    http.Handle("/ws", ws)
    http.Handle("/", http.FileServer(http.Dir("web")))

    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal("Speck:", err)
    }
}

Client

The client uses autobahn.js, a javascript WAMP client implementation. Speck clients make use of the PubSub features of WAMP, so all they have to do is subscribe to the "speck:data" topic and when any other client creates a "speck" they get an event with the details. I used kinetic.js to manage the canvas and handle the animations. I hadn't done anything with the HTML5 canvas before, but kinetic was really easy to use. The author of kinetic put together some really awesome tutorials for canvas, kinetic, and a few other libraries here. Here's an image of speck in action:

speck demo

Tags: programming, go, golang, websockets, javascript