Our great sponsors
-
nbio
Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
-
centrifuge
Real-time messaging library for Go. The simplest way to add feature-rich and scalable WebSocket support to your application. The core of Centrifugo server.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
There is also https://github.com/lesismal/nbio library which also implements somewhat similar to https://github.com/gobwas/ws - but with async websocket frame parser. Also shifts you from STD lib.
What I can say is that if you will use Gorilla Websocket (https://github.com/gorilla/websocket) (it's archived now unfortunately, but still super robust) - then you can go to sth like 15-20kb per connection - see https://github.com/FZambia/go_websocket_memory - to achieve this you need to reduce read buffer size, reuse write buffers, use goroutine after hijacking connection (following chat example in Gorilla WebSocket repo).
What I can say is that if you will use Gorilla Websocket (https://github.com/gorilla/websocket) (it's archived now unfortunately, but still super robust) - then you can go to sth like 15-20kb per connection - see https://github.com/FZambia/go_websocket_memory - to achieve this you need to reduce read buffer size, reuse write buffers, use goroutine after hijacking connection (following chat example in Gorilla WebSocket repo).
Then take a look at this article - https://www.freecodecamp.org/news/million-websockets-and-go-cc58418460bb/ - using external from Go std lib epoll implementation and https://github.com/gobwas/ws lib it's possible to reduce memory usage per connection drastically. Though keep in mind that this approach is not obvious to implement right and you better to go with STD lib.
Also take a look at https://github.com/nhooyr/websocket - which is a good ws library, but I don't have anything specific about its memory usage per connection. But from what I see it will be somewhat similar to x/net/websocket.
There are libraries which provide more advanced APIs for creating real-time applications, for example https://github.com/centrifugal/centrifuge
Would be great to have the ideas from nbio and gobwas/ws in standard library at some point. Because I personally worry to shift from std lib. Custom TLS implementation makes me nervous, things like QUIC will be not available when I decide I need them in the application, etc. Unfortunately seems that discussions which could make it happen (like this one https://github.com/golang/go/issues/15735) not proceeding these days.
Related posts
- "Every time a new Go release happened, the package stopped building, and the authors had to add a new file with a new //go:build line, and then the entire ecosystem of packages with that as a dependency had to explicitly update to the new version" -- Go itself
- Is there an alternative to gorilla websocket?
- Gorilla/websocket or Melody?
- Gorilla toolkit maintainers are stepping down and have been looking for new maintainers. The project could otherwise be archived.
- Building basic CRUD operations in Go with Fiber