# Ruby Websocket Services with Websocketd

Source: https://rickcarlino.com/2014/one-off-ruby-websocket-services-with-websocketd.html
Published: 2014-11-13

[Websocketd](http://websocketd.com/) is a program similar to
[xinetd](http://en.wikipedia.org/wiki/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:

```ruby
#!/Users/me/.rvm/rubies/ruby-2.1.2/bin/ruby

require 'stock_quote'

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

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

After running the command below...

```bash
chmod +x ./script.rb
```

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

```bash
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:

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

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