socket.io-go-parser

Overview
This is the Go parser for the Socket.IO protocol, responsible for encoding and decoding packets. It is shared by both socket.io-client-go and socket.io.
Compatibility Table
| Parser Version |
Socket.IO Server Version |
Protocol Revision |
| 3.x |
3.x |
5 |
Features
- Full support for Socket.IO protocol v5
- Encoding and decoding of packets
- Binary data support
- Event-based decoding
- Extensible and thread-safe implementation
Installation
To install the package, run:
go get github.com/technance-foundation/socket.io/parsers/socket/v3
Example Usage
Encoding and Decoding a Packet
package main
import (
"github.com/technance-foundation/socket.io/v3/pkg/utils"
"github.com/technance-foundation/socket.io/parsers/socket/v3/parser"
)
func main() {
encoder := parser.NewEncoder()
id := uint64(13)
packet := &parser.Packet{
Type: parser.EVENT,
Data: []string{"test-packet"},
Id: &id,
}
encodedPackets := encoder.Encode(packet)
utils.Log().Default("Encoded: %v", encodedPackets)
for _, encodedPacket := range encodedPackets {
decoder := parser.NewDecoder()
decoder.On("decoded", func(decodedPackets ...any) {
utils.Log().Default("Decoded: %v", decodedPackets[0])
// decodedPackets[0].Type == parser.EVENT
// decodedPackets[0].Data == []string{"test-packet"}
// decodedPackets[0].Id == 13
})
decoder.Add(encodedPacket)
}
}
Encoding and Decoding a Packet with Binary Data
package main
import (
"github.com/technance-foundation/socket.io/v3/pkg/utils"
"github.com/technance-foundation/socket.io/parsers/socket/v3/parser"
)
func main() {
encoder := parser.NewEncoder()
attachments := uint64(0)
packet := &parser.Packet{
Type: parser.BINARY_EVENT,
Data: []any{"test-packet", []byte{1, 2, 3, 4, 5}},
Id: utils.Ptr(uint64(13)),
Attachments: &attachments,
}
encodedPackets := encoder.Encode(packet)
utils.Log().Default("Encoded: %v", encodedPackets)
for _, encodedPacket := range encodedPackets {
decoder := parser.NewDecoder()
decoder.On("decoded", func(decodedPackets ...any) {
utils.Log().Default("Decoded: %v", decodedPackets[0])
// decodedPackets[0].Type == parser.BINARY_EVENT
// decodedPackets[0].Data == []any{"test-packet", []byte{1, 2, 3, 4, 5}}
// decodedPackets[0].Id == 13
})
decoder.Add(encodedPacket)
}
}
API Reference
Packet Structure
type Packet struct {
Type PacketType // Type of the packet (e.g., EVENT, BINARY_EVENT)
Data any // Packet data
Id *uint64 // Packet ID (optional)
Attachments *uint64 // Number of binary attachments (optional)
}
Encoder Interface
type Encoder interface {
Encode(packet *Packet) []types.BufferInterface
}
Decoder Interface
type Decoder interface {
types.EventEmitter
Add(data any) error
Destroy()
}
Tests
Run the test suite with:
make test
Development
To contribute to the project, follow these steps:
- Fork the repository.
- Create a feature branch:
git checkout -b feature/amazing-feature.
- Commit your changes:
git commit -m 'Add some amazing feature'.
- Push to the branch:
git push origin feature/amazing-feature.
- Open a Pull Request.
Support
If you encounter any issues or have questions, please file them in the issues section.
License
This project is licensed under the MIT License. See the LICENSE file for details.