Skip to content

Repository files navigation

Socket.IO V5 Default Event Payload Parser with Jsoniter

This package provides a high-performance JSON parser for Socket.IO V5 protocol event payload using the jsoniter library. It's designed to be a drop-in replacement for the default JSON parser, offering significant performance improvements.

License Go Report Card codecov Go Reference
Test Lint Sec CodeQL

Features

  • High-performance JSON parsing and serialization
  • Compatible with Socket.IO V5 default protocol parser
  • Easy integration with the default parser

Installation

To use this parser, first ensure you have the main Socket.IO client library installed:

go get github.com/maldikhan/go.socket.io

Then, install this parser:

go get github.com/maldikhan/go.socket.io-parser.jsoniter

Usage

To use this parser with the Socket.IO client, you need to create a new parser instance and pass it to the client constructor:

package main

import (
    "github.com/maldikhan/go.socket.io"
    socketio_v5_parser_default "github.com/maldikhan/go.socket.io/socket.io/v5/parser/default"
    socketio_v5_parser_default_jsoniter "github.com/maldikhan/go.socket.io-parser.jsoniter"
    "github.com/maldikhan/go.socket.io/utils"
)

func main() {
    client, err := socketio.NewClient(
        socketio.WithRawURL("http://localhost:3000"),
        socketio.WithParser(socketio_v5_parser_default.NewParser(
            socketio_v5_parser_default.WithLogger(&utils.DefaultLogger{Level: utils.WARN}),
            socketio_v5_parser_default.WithPayloadParser(socketio_v5_parser_default_jsoniter.NewPayloadParser(
                socketio_v5_parser_default_jsoniter.WithLogger(&utils.DefaultLogger{Level: utils.WARN}),
            )),
        )),
    )
    if err != nil {
        // Handle error
    }
    
    // Use the client as normal
    /*
        err := client.Connect(ctx)
        ...
    */
}

Performance

This parser can provide 2-4x faster parsing of payloads compared to the standard library's encoding/json. The exact performance gain may vary depending on your specific use case and payload structure.

Compatibility

This parser is designed to be fully compatible with the Socket.IO V5 protocol. It can be used as a drop-in replacement for the default parser without any changes to your existing code.

Contributing

Contributions to improve the parser are welcome. Please feel free to submit issues or pull requests.

Acknowledgments

  • Socket.IO for the original protocol and implementation
  • jsoniter for fast JSON parsing

License

This parser is licensed under the MIT License. See the LICENSE file in the root of the repository for the full license text.

Releases

Packages

Contributors

Languages