Introducing GoSocket – A Simple WebSocket Framework for Go

Hi Go community,

I’m excited to share GoSocket, a lightweight WebSocket library for Go that aims to make setting up WebSocket servers fast.

Setting up a WebSocket server in Go often requires writing a lot of boilerplate: handling connections, managing clients, broadcasting messages, dealing with rooms, and supporting different message formats. GoSocket abstracts all of that so you can get a working server running in just a few lines of code.

Features

  • Quick setup: 5–10 lines of code to get a server running
  • Multiple encoding support: JSON (ready), Protobuf & MessagePack (planned), or raw binary
  • Rooms & broadcasting: Join/leave rooms and broadcast messages easily
  • Middleware support: Authentication, logging, CORS, etc.
  • Graceful shutdown: Clean connection handling
  • Multiple servers: Run chat, notifications, and admin panels on different ports simultaneously

Quick Example

ws := gosocket.NewServer()

ws.WithPort(8080).
    WithPath("/ws").
    OnConnect(func(client *gosocket.Client, ctx *gosocket.HandlerContext) error {
        fmt.Printf("Client connected: %s\n", client.ID)
        return nil
    }).
    OnMessage(func(client *gosocket.Client, message *gosocket.Message, ctx *gosocket.HandlerContext) error {
        fmt.Printf("Received: %s\n", string(message.RawData))
        // Echo back
        client.Send(message.RawData)
        return nil
    }).
    OnDisconnect(func(client *gosocket.Client, ctx *gosocket.HandlerContext) error {
        fmt.Printf("Client disconnected: %s\n", client.ID)
        return nil
    })

log.Fatal(ws.Start())

For more examples, check out the examples folder.

Current Status

We’re planning to release v1.0.0 soon, but you can start testing pre-production versions today.

Contributing

GoSocket is actively being developed and we welcome contributions in:

  • Documentation & examples
  • Testing edge cases and performance scenarios
  • Adding new serializers (Protobuf, MessagePack)

If you’d like to contribute, check the code structure, open an issue to discuss what you want to work on, and start coding.

You can find the project on GitHub: GitHub - FilipeJohansson/gosocket: A simple WebSocket abstraction for Go that gets you up and running in minutes.

Any help testing, contributing, or even giving feedback is greatly appreciated. Looking forward to seeing what the community thinks!

Thank you :slight_smile:

Looks interesting. Can you provide some comparision to gorilla/websocket and to the standard-implementation?

What does this module do different? What are reasons/cases to choose one over the other?

1 Like

Thank you!

GoSocket is actually running on top of gorilla.
The problem GoSocket tries to solve is handling with rooms, clients, encodings, broadcast, events - GoSocket abstracts this for you.
So you can use, for example, gosocket.OnConnect(…) to do a logic when a client connects, or gosocket.OnMessage(…) for when a message ir received.

This can be used as a handler as well, so if you want to implement in a already running gorilla/gin/etc server, you can!
Using gosocket.NewHandler(…) will give you the abstractions for you while you’ll be able to do http.Handle("/ws", ws) (where ws is an instance of NewHandler()).

The project’s README (GitHub - FilipeJohansson/gosocket: A simple WebSocket abstraction for Go that gets you up and running in minutes.) have more examples if you’re interested.

I’m receiving some feedbacks and would love for more :slight_smile:

1 Like