Golang Tutorial on How to Use Websockets

Websockets with Golang

Sending a message and getting an instant response without refreshing the page is something we take for granted. But in the past, enabling real-time functionality was a real challenge for developers. The developer community has come a long way from HTTP long polling and AJAX and has finally found a solution for building truly real-time apps. 

This solution comes from WebSockets, which make it possible to open an interactive session between a user’s browser and a server. WebSockets allow a browser to send messages to a server and receive event-driven responses without polling the server for a reply.

For now, WebSockets are the number one solution for building real-time applications: online games, instant messengers, tracking apps, and so on. This guide explains how WebSockets operate and how we can build WebSocket applications in the Golang programming language. We also compare the most popular WebSocket libraries so you can choose the best one for your needs. 

Read also: Best Practices for Speeding Up JSON Encoding and Decoding in Go

Network sockets vs WebSockets

To discover how to get started with WebSockets in the GO, let’s draw the line between network sockets and WebSockets.

Network socket 

A network socket, or simply a socket, serves as an internal endpoint for exchanging data between applications running on the same computer or different computers on the same network.

Sockets are a crucial part of Unix and Windows-based operating systems, making it easier for developers to create network-enabled software. Instead of constructing network connections from scratch, app developers can include sockets in their programs. Since network sockets are used for several protocols (HTTP, FTP, etc.), multiple sockets can be used simultaneously.

Sockets are created and used with a set of function calls defined by a socket’s application programming interface (API). 

There are several types of network sockets:

Datagram sockets (SOCK_DGRAM), also known as connectionless sockets, use the User Datagram Protocol (UDP). Datagram sockets support a bidirectional flow of messages and preserve record boundaries.

Stream sockets (SOCK_STREAM), also known as connection-oriented sockets, use the Transmission Control Protocol (TCP), Stream Control Transmission Protocol (SCTP), or Datagram Congestion Control Protocol (DCCP). These sockets provide a bidirectional, reliable, sequenced, and unduplicated data flow with no record boundaries.

Raw sockets (or IP sockets) are typically available in routers and other networking equipment. These sockets are generally datagram-oriented, although their exact characteristics depend on the interface provided by the protocol. Most applications do not use raw sockets. They’re provided to support the development of new communication protocols and access to more esoteric facilities of existing protocols. 

Socket communication with Golang 

Each network socket is identified by the address, a triad of a transport protocol, IP address, and port number.

There are two significant protocols for communicating between hosts: TCP and UDP. Let’s see how your app can connect to TCP and UDP sockets. 

Connecting to a TCP socket

A Golang client uses the DialTCP function in the net package to establish a TCP connection. DialTCP returns a TCPConn object. When a connection is established, the client and server begin exchanging data: the client sends a request to the server through a TCPConn object, the server parses the request and sends a response, and the TCPConn object receives the response from the server. 
 

tcp socket