Posts List

Live Process Output To The User With Redis and Server-Sent Events

In a recent project I needed to collect the output from a process that was occurring on the server and output it to the user on the frontend. Ideally that would occur in nearly real time. The process I needed to collect output from is a third-party library that outputs everything to STDOUT or STDERR. The easiest way for me to accomplish that without shenanigans was to create a module to run the library and then fork the module as a child process and then pipe the output to a stream handler.

Server-Sent Events With Node

Server-Sent Events (SSEs) allow one-way communication from the server to the client. They can be very useful for things like notifications or activity feeds. I’ve recently used them in a project to display output from a background process in the browser. In the browser, you connect to the server using the EventSource interface and just add event listeners. It is really very easy. const sseSource = new EventSource('/event-stream'); sseSource.addEventListener('message', (e) => { const messageData = e.

String Length & Bytes In JavaScript

In most cases, you can assume one character in a string is 1 byte, but that is only in most cases. How many bytes do you think ü is? It turns out it is 2 bytes. But if you run 'ü'.length it will return the string’s length as 1. Unicode characters can appear as a single character but be made up of multiple bytes of data. Usually, this isn’t a big deal if you just need the length of a string, but if you actually need the size in bytes of a string it is a big deal.

Restarting Your Node App After Changes

A nice feature of tools like create-react-app, angular-cli, webpack, and many others is the ability to reload your application when you make a change to a source file. But what do you do when you have a Node backend to go along with your frontend? There are numerous tools you can use to detect changes to files and then restart a process based on that, but who has time to build those scripts to put it all together?