func wsHandler(ws *websocket.Conn) {
	log.Println("New client")
	defer ws.Close()
	for {
		var message string
		if err := websocket.Message.Receive(ws, &message); err != nil {
			log.Println("Error receiving message. Closing connection.")
			return
		}
		user, _ := common.FindUserByApiKey(message)
		if user != nil {
			log.Println("Registering apiKey", user.ApiKey)
			clients[user.ApiKey] = ws
		} else {
			log.Println("Error registering user", message)
		}
	}
}
Connection handler. This function called after new client
connected to websocket server.
Also this method performs register user - client must send valid apiKey
to receive messages from redis.
package web_socket
import (
	"encoding/json"
	"log"
	"net/http"
	"github.com/firstrow/logvoyage/common"
	"code.google.com/p/go.net/websocket"
	"github.com/garyburd/redigo/redis"
)
const (
	redisChannel = "ws"
)
type RedisMessage struct {
	ApiKey string
	Data   interface{}
}
func (m *RedisMessage) Send(r redis.Conn) (interface{}, error) {
	j, err := json.Marshal(m)
	if err != nil {
		return nil, err
	}
	return r.Do("PUBLISH", redisChannel, string(j))
}
var clients = make(map[string]*websocket.Conn)
func StartServer() {
	go startListetingRedis()
	http.Handle("/ws", websocket.Handler(wsHandler))
	err := http.ListenAndServe(":12345", nil)
	checkError(err)
}
func checkError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
func startListetingRedis() {
	c, err := redis.Dial("tcp", ":6379")
	checkError(err)
	c.Send("SUBSCRIBE", redisChannel)
	c.Flush()
	log.Println("Started server and connected to redis")
	psc := redis.PubSubConn{c}
	go func() {
		for {
			switch v := psc.Receive().(type) {
			case redis.Message:
				log.Printf("%s: message: %s\n", v.Channel, v.Data)
				var message RedisMessage
				err = json.Unmarshal(v.Data, &message)
				if err != nil {
					continue
				}
				if wsClient, ok := clients[message.ApiKey]; ok {
					j, _ := json.Marshal(message.Data)
					if err = websocket.Message.Send(wsClient, string(j)); err != nil {
						wsClient.Close()
						delete(clients, message.ApiKey)
						log.Println("Could not send message to ", wsClient, err.Error())
					}
				}
			case error:
				log.Println("Error occured with redis.", v)
			}
		}
	}()
}