socketio

package module
v4.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 16 Imported by: 0

README

golang socket.io

  • 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

Contents

Install

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.

Documents

Server

Constructor
socketio.New

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")
}
Events
Event: 'connection'
io.OnConnection(func(socket *socketio.Socket) {
	// ...
})
Methods
server.emit(eventName[, ...args])
io.Emit("hello")
io.Emit("hello", 1, "2", map[string]interface{}{"3": 4})
server.of(nsp)
adminNamespace := io.Of("/admin")

adminNamespace.OnConnection(func(socket *socketio.Socket) {
	// ...
})
server.to(room)
io.To("room-101").Emit("hello", "world")
server.fetchSockets()
sockets := io.Sockets()

Namespace

Events
Event: 'connection'

Fired upon a connection from client.

// main namespace
io.OnConnection(func(socket *socketio.Socket) {
	// ...
})

// custom namespace
io.Of("/admin").OnConnection(func(socket *socketio.Socket) {
	// ...
})
Methods
namespace.emit(eventName[, ...args])
io.Of("/admin").Emit("hello")
io.Of("/admin").Emit("hello", 1, "2", map[string]interface{}{"3": 4})
namespace.to(room)
adminNamespace := io.Of("/admin")

adminNamespace.To("room-101").Emit("hello", "world")

adminNamespace.To("room-101").To("room-102").Emit("hello", "world")
namespace.fetchSockets()

Returns the matching Socket instances:

adminNamespace := io.Of("/admin")

sockets := adminNamespace.Sockets()

Socket

Events
Event: 'disconnect'
io.OnConnection(func(socket *socketio.Socket) {
	socket.On("disconnect", func(event *socketio.EventPayload) {
		// ...
	})
})
Methods
socket.on(eventName, callback)

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",
		})
	}
})
socket.join(room)

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")
})
socket.leave(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.to(room)
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")
})

Example

Please check more examples into folder in project for details. Examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorInvalidConnection = errors.New("invalid connection")
	ErrorUUIDDuplication   = errors.New("UUID already exists")
)

Functions

This section is empty.

Types

type AckCallback

type AckCallback func(data ...interface{})

type Conn added in v4.0.6

type Conn struct {
	// contains filtered or unexported fields
}

func (*Conn) Close added in v4.0.6

func (c *Conn) Close() error

func (*Conn) NextWriter added in v4.0.6

func (c *Conn) NextWriter(messageType int) (io.WriteCloser, error)

func (*Conn) SetReadDeadline added in v4.0.6

func (c *Conn) SetReadDeadline(t time.Time) error

type EventPayload

type EventPayload struct {
	Name   string //event name
	SID    string //socket id
	Socket *Socket
	Error  error
	Data   []interface{}
	Ack    AckCallback
}

type Io

type Io struct {
	// contains filtered or unexported fields
}

func New

func New() *Io

func (*Io) Close

func (s *Io) Close()

func (*Io) Emit

func (s *Io) Emit(event string, agrs ...interface{}) error

func (*Io) FiberMiddleware added in v4.0.7

func (s *Io) FiberMiddleware(c *fiber.Ctx) error

func (*Io) FiberRoute added in v4.0.7

func (s *Io) FiberRoute(router fiber.Router)

func (*Io) HttpHandler added in v4.0.6

func (s *Io) HttpHandler() http.Handler

func (*Io) Middleware

func (s *Io) Middleware(c *fiber.Ctx) error

func (*Io) Of

func (s *Io) Of(name string) *Namespace

func (*Io) OnAuthorization

func (s *Io) OnAuthorization(fn func(params map[string]string) bool)

func (*Io) OnConnection

func (s *Io) OnConnection(fn connectionEventCallback)

func (*Io) ServeHTTP added in v4.0.6

func (s *Io) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Io) Server

func (s *Io) Server(router fiber.Router)

func (*Io) Sockets

func (s *Io) Sockets() []*Socket

func (*Io) To

func (s *Io) To(name string) *Room

type Namespace

type Namespace struct {
	Name string
	// contains filtered or unexported fields
}

func (*Namespace) Emit

func (nps *Namespace) Emit(event string, agrs ...interface{}) error

func (*Namespace) OnConnection

func (nps *Namespace) OnConnection(fn connectionEventCallback)

func (*Namespace) Sockets

func (nps *Namespace) Sockets() []*Socket

func (*Namespace) To

func (nps *Namespace) To(room string) *Room

type Room

type Room struct {
	Name string
	To   func(room string) *Room
	// contains filtered or unexported fields
}

func (*Room) Emit

func (room *Room) Emit(event string, agrs ...interface{}) error

func (*Room) Sockets

func (room *Room) Sockets() []*Socket

type Socket

type Socket struct {
	Id   string
	Nps  string
	Conn *Conn

	Join  func(room string)
	Leave func(room string)
	To    func(room string) *Room
	// contains filtered or unexported fields
}

func (*Socket) Disconnect

func (s *Socket) Disconnect() error

func (*Socket) Emit

func (s *Socket) Emit(event string, agrs ...interface{}) error

func (*Socket) On

func (s *Socket) On(event string, fn eventCallback)

func (*Socket) Ping

func (s *Socket) Ping() error

func (*Socket) Rooms

func (s *Socket) Rooms() []string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL