go-socket.io is an implementation of Socket.IO in Golang, which is a realtime application framework.
Currently this library supports 1.4 version of the Socket.IO client. It supports room and namespaces.
- for compatibility with Socket.IO 0.9.x, please use branch 0.9.x *
Install the package with:
go get github.com/MathCody/go-socket.io
Import it with:
import "github.com/MathCody/go-socket.io"
and use socketio
as the package name inside the code.
Please check the example folder for details.
package main
import (
"fmt"
"log"
"net/http"
"github.com/googollee/go-socket.io"
)
func main() {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
fmt.Println("connected:", s.ID())
return nil
})
server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
fmt.Println("notice:", msg)
s.Emit("reply", "have "+msg)
})
server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string {
s.SetContext(msg)
return "recv " + msg
})
server.OnEvent("/", "bye", func(s socketio.Conn) string {
last := s.Context().(string)
s.Emit("bye", last)
s.Close()
return last
})
server.OnError("/", func(e error) {
fmt.Println("meet error:", e)
})
server.OnDisconnect("/", func(s socketio.Conn, msg string) {
fmt.Println("closed", msg)
})
go server.Serve()
defer server.Close()
http.Handle("/socket.io/", server)
http.Handle("/", http.FileServer(http.Dir("./asset")))
log.Println("Serving at localhost:8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}
See documentation about acknowledgements
- Client-side
//using client-side socket.io-1.X.X.js
socket.emit('some:event', JSON.stringify(someData), function(data){
console.log('ACK from server wtih data: ', data));
});
- Server-side
// The return type may vary depending on whether you will return
// In golang implementation of Socket.IO don't used callbacks for acknowledgement,
// but used return value, which wrapped into ack package and returned to the client's callback in JavaScript
so.On("some:event", func(msg string) string {
return msg //Sending ack with data in msg back to client, using "return statement"
})
- Client-side
//using client-side socket.io-1.X.X.js
//last parameter of "on" handler is callback for sending ack to server with data or without data
socket.on('some:event', function (msg, sendAckCb) {
//Sending ACK with data to server after receiving some:event from server
sendAckCb(JSON.stringify(data)); // for example used serializing to JSON
}
- Server-side
//You can use Emit or BroadcastTo with last parameter as callback for handling ack from client
//Sending packet to room "room_name" and event "some:event"
so.BroadcastTo("room_name", "some:event", dataForClient, func (so socketio.Socket, data string) {
log.Println("Client ACK with data: ", data)
})
// Or
so.Emit("some:event", dataForClient, func (so socketio.Socket, data string) {
log.Println("Client ACK with data: ", data)
})
The 3-clause BSD License - see LICENSE for more details