- socket.io is library an implementation of Socket.IO in Golang, which is a realtime application framework.
- This library support socket.io-client version 3, 4 and only support websocket transport
Install the package with:
go get github.com/doquangtan/socket.io/v4
Import it with:
import "github.com/doquangtan/socket.io/v4"
and use socketio
as the package name inside the code.
Using with standard library net/http
import (
"net/http"
socketio "github.com/doquangtan/socket.io/v4"
)
func main() {
io := socketio.New()
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
http.Handle("/socket.io/", io.HttpHandler())
http.ListenAndServe(":3000", nil)
}
Using with web-framework Gin
import (
"github.com/gin-gonic/gin"
socketio "github.com/doquangtan/socket.io/v4"
)
func main() {
io := socketio.New()
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
router := gin.Default()
router.GET("/socket.io/", gin.WrapH(io.HttpHandler()))
router.Run(":3300")
}
Using with web-framework Go Fiber
import (
"github.com/gofiber/fiber/v2"
socketio "github.com/doquangtan/socket.io/v4"
)
func main() {
io := socketio.New()
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
app := fiber.New(fiber.Config{})
app.Use("/", io.FiberMiddleware) //This middleware is to attach socketio to the context of fiber
app.Route("/socket.io", io.FiberRoute)
app.Listen(":3000")
}
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
io.Emit("hello")
io.Emit("hello", 1, "2", map[string]interface{}{"3": 4})
adminNamespace := io.Of("/admin")
adminNamespace.OnConnection(func(socket *socketio.Socket) {
// ...
})
io.To("room-101").Emit("hello", "world")
sockets := io.Sockets()
Fired upon a connection from client.
// main namespace
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
// custom namespace
io.Of("/admin").OnConnection(func(socket *socketio.Socket) {
// ...
})
io.Of("/admin").Emit("hello")
io.Of("/admin").Emit("hello", 1, "2", map[string]interface{}{"3": 4})
adminNamespace := io.Of("/admin")
adminNamespace.To("room-101").Emit("hello", "world")
adminNamespace.To("room-101").To("room-102").Emit("hello", "world")
Returns the matching Socket instances:
adminNamespace := io.Of("/admin")
sockets := adminNamespace.Sockets()
io.OnConnection(func(socket *socketio.Socket) {
socket.On("disconnect", func(event *socketio.EventPayload) {
// ...
})
})
Register a new handler for the given event.
socket.On("news", func(event *socketio.EventPayload) {
print(event.Data)
})
with several arguments
socket.On("news", func(event *socketio.EventPayload) {
if len(event.Data) > 0 && event.Data[0] != nil {
print(event.Data[0])
}
if len(event.Data) > 1 && event.Data[1] != nil {
print(event.Data[1])
}
if len(event.Data) > 2 && event.Data[2] != nil {
print(event.Data[2])
}
})
or with acknowledgement
socket.On("news", func(event *socketio.EventPayload) {
if event.Ack != nil {
event.Ack("hello", map[string]interface{}{
"Test": "ok",
})
}
})
Adds the socket to the given room or to the list of rooms.
io.Of("/test").OnConnection(func(socket *socketio.Socket) {
socket.Join("room 237")
io.To("room 237").Emit("a new user has joined the room")
})
Removes the socket from the given room.
io.Of("/test").OnConnection(func(socket *socketio.Socket) {
socket.Leave("room 237");
io.To("room 237").Emit("the user has left the room")
})
Rooms are left automatically upon disconnection.
socket.On("room 237", func(event *socketio.EventPayload) {
// to one room
socket.To("room 237").Emit("test", "hello")
// to multiple rooms
socket.To("room 237").To("room 123").Emit("test", "hello")
})
Please check more examples into folder in project for details. Examples