...

/

WebSocket Implementation

WebSocket Implementation

Here we start implementing WebSockets for server communication.

We'll cover the following...

As with any infrastructure-related code, middleware is the perfect place for our WebSocket implementation. Using middleware will allow us to catch any required actions sent over the network and dispatch anything coming from the server. The basic WebSocket setup is as follows:

// Basic structure of a WebSocket setup
const WS_ROOT = "ws://echo.websocket.org/";

const websocket = new WebSocket(WS_ROOT);

websocket.onopen    = () => {};
websocket.onclose   = () => {};
websocket.onerror   = event => {};
websocket.onmessage = event => {};

To make the code more readable, we can replace ...