Rick Carlino

Lead software developer and a co-founding member at FarmBot

Co-founder and Maker of Things at Fox.Build Makerspace in St. Charles, IL.

[ 📨 Newsletter ] [ 💬 Contact ] [ 👤 About ]

Ruby Websocket Services with Websocketd

Websocketd is a program similar to xinetd which allows users to host any command line application over a websocket by simply reading and writing to STDIN/STDOUT.

Let’s say we wanted to stream the AAPL stock quote to a live dashboard

Example code:


require 'stock_quote'

STDOUT.puts "Welcome! Streaming AAPL stock price now..."

while true
  STDOUT.puts StockQuote::Stock.quote("aapl").ask
  sleep 2

After running the command below…

chmod +x ./script.rb

…the Ruby snippet could be served to a browser by running the following:

websocketd --port=8080 ./script.rb

That’s all there is to it. You can now connect using a Websocket from any modern browser.

Try running this from your JS console to see all incoming messages from the server:

// Connect
var ws = new WebSocket("ws://localhost:8080");

// Print all incoming messages to console
ws.onmessage = function(e) { console.log(e); };