Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is websocket.Upgrader exactly?

Tags:

websocket

go

I am trying to learn about websockets, and I am not sure I understand what exactly Upgrader does in gorilla/websockets.

http://www.gorillatoolkit.org/pkg/websocket#Upgrader

Can someone please explain in simple terms what exactly the buffer sizes mean?

like image 403
user1354934 Avatar asked May 06 '18 22:05

user1354934


People also ask

What is a WebSocket in simple terms?

WebSocket is a communications protocol for a persistent, bi-directional, full duplex TCP connection from a user's web browser to a server. A WebSocket connection is initiated by sending a WebSocket handshake request from a browser's HTTP connection to a server to upgrade the connection.

What is WebSocket and how it works?

In WebSocket, communication occurs at both ends, which makes it a faster protocol. In HTTP, the connection is built at one end, making it a bit sluggish than WebSocket. WebSocket uses a unified TCP connection and needs one party to terminate the connection. Until it happens, the connection remains active.

What is the purpose of a WebSocket?

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

What is the difference between WS and WSS?

The wss protocol establishes a WebSocket over an encrypted TLS connection, while the ws protocol uses an unencrypted connection. At this point, the network connection remains open and can be used to send WebSocket messages in either direction.


Video Answer


1 Answers

The Upgrader.Upgrade method upgrades the HTTP server connection to the WebSocket protocol as described in the WebSocket RFC. A summary of the process is this: The client sends an HTTP request requesting that the server upgrade the connection used for the HTTP request to the WebSocket protocol. The server inspects the request and if all is good the server sends an HTTP response agreeing to upgrade the connection. From that point on, the client and server use the WebSocket protocol over the network connection.

Applications use Upgrader fields to specify options for the upgrade operation.

The WebSocket connection buffers reads and writes to the underlying network connection. The ReadBufferSize and WriteBufferSize specifies the size of these buffers. It's usually best to use the default size by setting ReadBufferSize and WriteBufferSize to zero. Larger buffer sizes take more memory. Smaller buffer sizes can result in more calls to the underlying network connection. The buffer sizes do not limit the size of a message that can be read.

like image 153
Bayta Darell Avatar answered Sep 18 '22 05:09

Bayta Darell