Get full bidirectional real-time communication for your application by implementing a WebSockets server with Deno.

WebSockets is a communications protocol that allows bidirectional, persistent communication as an alternative to HTTP. Suppose you wanted to get updates to some server state. With HTTP, you would need to poll frequently for the data.

But there are limitations to HTTP polling:

  1. Changes to data aren’t seen in real time by subscribing clients
  2. The server only responds to an initial request from the client — in other words, it’s unidirectional
  3. Server resources are tied up processing requests even when there’s no new data

With WebSockets, clients don’t need to ask the server for new data, the web sockets server simply pushes the new data directly to the client.

When should you use WebSockets?

WebSockets should be used when you need real-time functionality in your application — for example, a chat application or a bot that watches the stock market. WebSockets are best used in situations where you need to react quickly to frequently changing data. If the your application data doesn’t change often, it may be best to implement simple polling logic instead.

How does the WebSockets protocol work?

WebSockets use HTTP as the mechanism to initiate a connection to the server. This connection is then upgraded to a WebSocket connection.

Clients can only access WebSocket servers through a URI scheme of either ws:// or wss://. To initiate a WebSockets connection, you must first implement a WebSocket client and have a supporting WebSockets server. That’s where Deno comes in.

Implementing a WebSocket server and client with Deno

Here’s what we’ll cover in this tutorial:

  1. Creating a WebSocket server in Deno
  2. Creating a WebSocket client in Deno
  3. Sending messages between the server and client
  4. Broadcasting messages from the server to multiple clients

#websocket #deno

How to Implement a WebSocket Server and Client with Deno
3.30 GEEK