Clone this repository
Mount the repository & run this command to install dependencies
make setup
make run
localhost:8080After running the server, open your Websocket client. If you don't have any, try Websocket King extension for chrome.
Connect to ws://localhost:8080/socket, you will be greeted by the server.
Server: Welcome! Your ID is f0ab664a-5af3-4f8d-8afe-eb93085267e4
{
"action": "subscribe",
"topic": "world"
}
{
"action": "publish",
"topic": "world",
"message": "Hello world!"
}
{
"action": "unsubscribe",
"topic": "world"
}
You might be asking "why should I use Websocket instead of REST API"?
REST API uses HTTP which can only send response once per request.
Meanwhile, WebSocket can be used for persistent bidirectional communication without the need of reestablishing connection everytime.
This can be useful in some scenario like chatting or pub-sub.
Here is the diagram to visualize the difference between HTTP and WebSocket.

This flowchart describes how this server works.

cmd
└── main
└── main.go
internal
└── websocket
└── handler.go
└── model.go
└── server.go
main.go: the main file to be executed.
handler.go: handles open/close connection & pass the message to the server.
model.go: stores the models used by the server.
server.go: runs specific action according to the client message, also containes functions that needed by the server to work properly as a websocket server.
This repository is far from ideal. It's just a proof-of-concept.
While this repository is close to a pub-sub, it can still be used for a chat server.
For example, we can add a function on socket connected so that client will be automatically subscribes to their own ID as a topic. Other clients then will use those user's ID as a topic to publish a message.
I have tried building such solution combined with Authorization to prevent other user from subscribing to other's ID and it does work.
This repository is inspired by Golang-PubSub by @tabvn