Git Product home page Git Product logo

unlimitedws's Introduction

Unlimited Websocket

Unlimitedws is a tiny, well-tested library which helps you build a robust Websocket server with a few lines of code. Specifically, the Websocket server can handle up to several million concurrent connections with only a few GBs of memory. The library has successfully done that by utilizing async IO, careful tuning OS, zero-upgrade TCP connections, efficient buffer reuse and worker pool in case of DDOS.

Prerequisites

  1. Linux OS
  2. Some MBs of memory :)

Installation

go get github.com/hxt365/unlimitedws

Usage

A robust Echo Websocket system, that handles 100k concurrent connections within only 200 MBs of memory, can be implemented as follows:

server, _ := unlimitedws.DefaultServer(":8000")

// OnConnect handles logic when a connection comes up
server.OnConnect = func(conn net.Conn) {
    fmt.Println("welcome", nameConn(conn))
}

// OnRead handles logic when a connection sends a message
server.OnRead = func(conn net.Conn) error {
    msg, op, err := wsutil.ReadClientData(conn)
    if err != nil {
        return err
    }
    err = wsutil.WriteServerMessage(conn, op, msg)
    if err != nil {
        return err
    }
    fmt.Println(string(msg))
    return nil
}

// OnClose handles logic when a connection gets closed
server.OnClose = func(conn net.Conn) {
    fmt.Println("goodbye", nameConn(conn))
}

server.Run()

Full example: Echo.

Built With

Unlimitedws is built on top of the excellent Gobwas library. It is highly recommended to read the documentation of that library for advanced usage.

Planning features

  1. Custom Cookie handler, for authentication purpose.
  2. Close orphan connections.
  3. Websocket fallbacks.
  4. Sending missing messages when reconnecting.
  5. Horizontal scaling.

Motivation

  1. Scaling WebSocket in Go and beyond article by Alexander Emelin.
  2. A Million WebSockets and Go article by Sergey Kamardin.
  3. Going Infinite, handling 1 millions websockets connections in Go video by Eran Yanay.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

unlimitedws's People

Contributors

hxt365 avatar

Stargazers

moguf avatar Nhannht avatar  avatar Tien Loc Le avatar Nguyen Van Luong avatar Dinh Cuong avatar lk avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.