Skip to content
This repository

A Socket.IO backend implementation written in Go

branch: master
Octocat-spinner-32 example Example: use the new json annotation tags September 14, 2011
Octocat-spinner-32 .gitignore Introduced a custom ServeMux to establish compatibility with the offi… February 12, 2011
Octocat-spinner-32 .gitmodules Example: updated client to 0.6.3 and made it point to the new repo September 13, 2011
Octocat-spinner-32 EXPERIMENTAL the state of the project November 17, 2010
Octocat-spinner-32 LICENSE initial import May 18, 2010
Octocat-spinner-32 Makefile Introduced a custom ServeMux to establish compatibility with the offi… February 12, 2011
Octocat-spinner-32 README.md Edited README.md via GitHub September 08, 2011
Octocat-spinner-32 client.go Client: fix compatibility with SIOCodec February 25, 2011
Octocat-spinner-32 codec.go restructure November 17, 2010
Octocat-spinner-32 codec_sio.go extracted message and codec patches from larger pull req August 26, 2011
Octocat-spinner-32 codec_sio_test.go test: drop json key tags August 27, 2011
Octocat-spinner-32 codec_siostreaming.go extracted message and codec patches from larger pull req August 26, 2011
Octocat-spinner-32 codec_siostreaming_test.go test: drop json key tags August 27, 2011
Octocat-spinner-32 config.go Introduced a custom ServeMux to establish compatibility with the offi… February 12, 2011
Octocat-spinner-32 connection.go Conn: add RemoteAddr() method. Fixes #1 April 18, 2011
Octocat-spinner-32 doc.go Update docs February 12, 2011
Octocat-spinner-32 message.go extracted message and codec patches from larger pull req August 26, 2011
Octocat-spinner-32 servemux.go Introduced a custom ServeMux to establish compatibility with the offi… February 12, 2011
Octocat-spinner-32 session.go restructure November 17, 2010
Octocat-spinner-32 socketio.go test: drop json key tags August 27, 2011
Octocat-spinner-32 socketio_test.go Introduced a custom ServeMux to establish compatibility with the offi… February 12, 2011
Octocat-spinner-32 transport.go restructure November 17, 2010
Octocat-spinner-32 transport_flashsocket.go restructure November 17, 2010
Octocat-spinner-32 transport_htmlfile.go Use http.Hijacker (for build, new Go release) March 07, 2011
Octocat-spinner-32 transport_jsonppolling.go Use http.Hijacker (for build, new Go release) March 07, 2011
Octocat-spinner-32 transport_websocket.go websocket: Draft75Handler is obsolete August 27, 2011
Octocat-spinner-32 transport_xhrmultipart.go fixes for handling "Origin" Header September 08, 2011
Octocat-spinner-32 transport_xhrpolling.go fixes for handling "Origin" Header September 08, 2011
Octocat-spinner-32 util.go restructure November 17, 2010
README.md

go-socket.io

The socketio package is a simple abstraction layer for different web browser- supported transport mechanisms. It is fully compatible with the Socket.IO client (version 0.6) JavaScript-library by LearnBoost Labs. By writing custom codecs the socketio could be perhaps used with other clients, too.

It provides an easy way for developers to rapidly prototype with the most popular browser transport mechanism today:

Compatibility with Socket.IO 0.7->

Go-socket.io is currently compatible with Socket.IO 0.6 clients only.

Demo

The Paper Experiment

Crash course

The socketio package works hand-in-hand with the standard http package (by plugging itself into http.ServeMux) and hence it doesn't need a full network port for itself. It has an callback-style event handling API. The callbacks are:

  • SocketIO.OnConnect
  • SocketIO.OnDisconnect
  • SocketIO.OnMessage

Other utility-methods include:

  • SocketIO.ServeMux
  • SocketIO.Broadcast
  • SocketIO.BroadcastExcept
  • SocketIO.GetConn

Each new connection will be automatically assigned an session id and using those the clients can reconnect without losing messages: the server persists clients' pending messages (until some configurable point) if they can't be immediately delivered. All writes are by design asynchronous and can be made through Conn.Send. The server also abstracts handshaking and various keep-alive mechanisms.

Finally, the actual format on the wire is described by a separate Codec. The default bundled codecs, SIOCodec and SIOStreamingCodec are fully compatible with the LearnBoost's Socket.IO client (master and development branches).

Example: A simple chat server

package main

import (
    "http"
    "log"
    "socketio"
)

func main() {
    sio := socketio.NewSocketIO(nil)

    sio.OnConnect(func(c *socketio.Conn) {
        sio.Broadcast(struct{ announcement string }{"connected: " + c.String()})
    })

    sio.OnDisconnect(func(c *socketio.Conn) {
        sio.BroadcastExcept(c,
            struct{ announcement string }{"disconnected: " + c.String()})
    })

    sio.OnMessage(func(c *socketio.Conn, msg socketio.Message) {
        sio.BroadcastExcept(c,
            struct{ message []string }{[]string{c.String(), msg.Data()}})
    })

    mux := sio.ServeMux()
    mux.Handle("/", http.FileServer("www/", "/"))

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

tl;dr

You can get the code and run the bundled example by following these steps:

$ git clone git://github.com/madari/go-socket.io.git
$ cd go-socket.io
$ git submodule update --init --recursive
$ make install
$ cd example
$ make
$ ./example

License

(The MIT License)

Copyright (c) 2011 Jukka-Pekka Kekkonen <karatepekka@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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 OR COPYRIGHT HOLDERS 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.

Something went wrong with that request. Please try again.