Understanding Bare Websocket Connections in Phoenix 1.7

Understanding Bare Websocket Connections in Phoenix 1.7
Benjamin Milde discusses the updates made in the Phoenix 1.7 release that now allow developers to work directly with bare websocket connections without resorting to the Phoenix.Channel abstraction or directly interacting with the cowboy or Bandit web servers, which was the case prior to release 1.7. The Phoenix and Plug teams, along with Mat Trudel, introduced two new libraries - WebSock and WebSockAdapter. These libraries encapsulate the behaviors and implementations for working with websocket connections in a similar manner as Plug does for HTTP connections. The article outlines the ease with which an HTTP request can be upgraded to a websocket connection and how the tailoring of requests, including authentication, can be achieved through routers and plugs before the websocket upgrade. It explains the implementation process of the WebSock behavior within a Phoenix application, demonstrating interactivity through a sample connection timer server that sends messages back to the client. Finally, Milde compares bare websockets with Phoenix.Channels and suggests scenarios where opting for bare websockets might be advantageous, such as when pubsub is not required or the client only supports basic websocket functionality.

© HashMerge 2024