Sling Academy
Home/Golang/Handling Binary Data Over WebSockets in Go for File Transfers

Handling Binary Data Over WebSockets in Go for File Transfers

Last updated: November 26, 2024

WebSockets are a powerful tool for real-time communication between clients and servers. They can be leveraged for various applications, including transferring binary data like files. In this article, we'll explore how to handle binary data over WebSockets in Go, providing code snippets and explanations to build a robust file transfer system.

Setting Up the Environment

Before we begin, ensure you have Go installed on your system. You can download it from the official Go website.

Install WebSocket Package

To work with WebSockets in Go, we need to use a WebSocket library. For this guide, we'll use gorilla/websocket:

go get -u github.com/gorilla/websocket

Creating the WebSocket Server

Let's start by setting up a basic WebSocket server that can handle binary data transfers:

package main

import (
    "log"
    "net/http"
    "github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
    // Allow all connections by default
    CheckOrigin: func(r *http.Request) bool {
        return true
    },
}

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.Println(err)
        return
    }
    defer ws.Close()

    for {
        messageType, data, err := ws.ReadMessage()
        if err != nil {
            log.Println("read:", err)
            break
        }
        if messageType == websocket.BinaryMessage {
            log.Printf("Received binary message of length %d", len(data))
            // Here you can add logic to handle the binary data
            continue
        }
    }
}

func main() {
    http.HandleFunc("/ws", handleConnections)
    log.Println("Server starting on :8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

In this code, we create a simple server that listens for WebSocket connections on the "/ws" endpoint. The handleConnections function upgrades HTTP requests to WebSocket connections and waits for binary messages, printing the length of received messages.

Client-Side Binary Data Transmission

Now, let’s look at how a client might send binary data (e.g., a file) through the WebSocket to our server. Here’s a simple JavaScript client example:

const socket = new WebSocket("ws://localhost:8080/ws");

// Open the WebSocket connection
socket.onopen = function(event) {
  console.log("WebSocket is open now.");
};

// Handle messages received from the server
socket.onmessage = function(event) {
  console.log("Message from server ", event.data);
};

// Send binary data (File) to the server
function sendFile(file) {
  const reader = new FileReader();
  reader.onload = function(event) {
    const arrayBuffer = event.target.result;
    socket.send(arrayBuffer);
  };
  reader.readAsArrayBuffer(file);
}

In the above client JavaScript, we establish a WebSocket connection and define a function sendFile to convert a file into an ArrayBuffer and transmit it via the WebSocket.

Conclusion

Using WebSockets for file transfers in Go allows for efficient and real-time data communication. With the above setup, you can create a framework to build more complex WebSocket-based applications, capable of sending and receiving files seamlessly. This pattern works well for applications requiring near-instantaneous uploads and downloads, enabling a smoother user experience.

Next Article: Testing WebSocket Applications in Go: Tools and Techniques

Previous Article: Implementing Role-Based Access Control for WebSocket Applications in Go

Series: Websocket & Chat Programs in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant