Note - This tutorial was written using Go version 1.9 and googollee/go-socket.io
Websockets are something I find interesting in the sense that they provide us with an alternative option to communication between applications as opposed to the standard RESTful API solution. With Sockets we can do cool things such as real-time communication between thousands to hundreds of thousands of different clients without having to incur the expense of hundreds of thousands of RESTful API calls hitting our servers every minute.
Real Life Example
To put things into perspective and show how important websockets can be. Imagine we had a chat application that got all the latest messages from a single server and pushed all new messages to that same server.
REST API Approach
- In order to attain real-time chat, you would have to poll the REST API that provides new messages every second.
- This would equate to roughly 60 REST API calls per minute per client.
- If we build a successful service and we start seeing more and more traffic hitting our application, our servers will start being inundated with handling the millions of REST API calls per minute.
If we then considered the scenario in which we used websockets instead of REST API calls:
- Each client would maintain one solitary connection to the server.
- With 1,000 clients we would only have to maintain 1,000 socket connections.
- If someone posts a new message, only then would our server
pushout an update to our 1,000 clients.
With this method we have severely minimized the amount of network traffic hitting our server. We’ve saved costs on the number of instances of our server application that we need to run and we can essentially handle thousands more clients without too much effort.
Implementing a Golang Server
In order to implement websockets in Go we have a number of different options. I come from a frontend background and one of the most prevalent libraries for socket communication in the frontend is socket-io and as such we’ll be using the Golang equivalent in order to ease integrating them together.
We can install the package using the
go get command like so:
And then we can include it within our go programs like so:
Let’s have a look at the example code that the provide in the
readme.md for the library that we are using.
Breaking it down
So in the above code sample we do everything within our
main() function. We first define a new
socketio server instance by calling
socketio.NewServer(nil) before defining what behaviors we want our socket server to have when connected to and when there is an error.
server.On('connection',...) we first log that there has been a successful connection made before then joining the
chat room using
After that we then specify what we want to happen when we receive a
"chat message" event through one of our connected sockets. Whenever our server receives this type of event we call
so.BroadcastTo("chat", "chat message", msg) which broadcasts whatever message was sent to every socket that is currently connected. This means that one client will see any messages that another client were to send.
Finally we define what should happen on
"disconnection", in this instance we simply just log the fact that one of our clients has disconnected.
A Frontend Client
Ok, so we have managed to flesh out our backend Go-based WebSocket server, but now it’s time to get a simple frontend application up and running so that we can test what we have done works.
We’ll start by creating a simple
index.html within our project directory.
When you then run your go websocket server by calling:
This should then start running on
http://localhost:5000. You should then be able to navigate to this URL within your browser and see new connections being made in the log output of your server.
You have now successfully built up a frontend that connects directly into your newly created backend websocket server!
Note - The full source code for this project can be found on Github: TutorialEdge/Go
If you found this tutorial useful or require any further assistance then please feel free to let me know in the comments section below.