This tutorial aims to guide you through the process of implementing WebSockets in Go for creating real-time applications. By the end, we will have built a simple chat application that demonstrates real-time data communication.
WebSockets is a communication protocol that provides full-duplex communication channels over a single TCP connection. Unlike HTTP where the client initiates requests, with WebSockets, the server can push messages to clients whenever it decides.
Go has a powerful package called gorilla/websocket
to work with WebSockets. To install it, run the following command:
go get github.com/gorilla/websocket
We will use the Upgrader
type provided by this package, which takes an HTTP connection and upgrades it to a WebSocket connection.
Here is an example of establishing a WebSocket connection:
var upgrader = websocket.Upgrader{}
func handleConnections(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a WebSocket
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
// Close the connection when the function returns
defer ws.Close()
}
For real-time communication, we can use the ReadMessage
and WriteMessage
methods provided by the WebSocket connection. Here's a simple example:
for {
messageType, message, err := ws.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = ws.WriteMessage(messageType, message)
if err != nil {
log.Println("write:", err)
break
}
}
Let's build a simple chat application. We'll start by setting up a WebSocket server.
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
func main() {
// Create a simple file server
fs := http.FileServer(http.Dir("../public"))
http.Handle("/", fs)
// Configure WebSocket route
http.HandleFunc("/ws", handleConnections)
// Start the server
log.Println("http server started on :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func handleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
for {
messageType, message, err := ws.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = ws.WriteMessage(messageType, message)
if err != nil {
log.Println("write:", err)
break
}
}
}
In this tutorial, we learned about WebSockets and how to implement them in Go. We also built a simple chat application to demonstrate real-time data communication.
gorilla/websocket
packageRemember, practice is key to mastering any new concept. Happy coding!