0.01
No commit activity in last 3 years
No release in over 3 years
Pipe data from a Ruby IO stream to a Websocket server, broadcasting to all clients.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

< 0.6.0, >= 0.5.1
 Project Readme

websocket-pipe

Pipe IO to a websocket, broadcast to clients

websocket-pipe allows you to pipe Ruby IO streams (files, stdin/stdout, sockets, etc) to a websocket server. The server will broadcast your message to all connected clients.

Installation

gem install websocket-pipe

API

::new(reader[,host_info])

Creates a new WebsocketPipe reading from the supplied IO object reader. Optional host_info will be passed to EM::WebSocket.run.

::fork!

Forks your websocket server off in a new process. Return value is a tuple of the process pid and a writer stream. The associated reader is used by the websocket process to broadcast data from writer to connected clients.

#start!

Starts the server.

Example

Tailing system.log

In the example below we'll pipe the output of a tail call to our browser, which will display the output as an unordered list.

Pipe STDIN to clients (stdin-to-ws.rb)

require 'websocket-pipe'

WebsocketPipe.new(STDIN).start!

Pipe tail output to clients (tail-syslog)

#!/usr/bin/env bash

tail -f /private/var/log/system.log | ruby stdin-to-ws.rb

Display received data from tail as UL (tail.html)

<html>
  <head>
    <title>Pipe</title>
    <style>
      #log { font-family: Anonymous Pro, monospace}
    </style>
  </head>
  <body>
    <ul id="log">
    </ul>
  </body>

  <script>
    var socket = new WebSocket('ws://localhost:8080');

    socket.onmessage = function(event) {
      var msg = document
        .createElement("li")
      msg.innerText = event.data
      document
        .getElementById("log")
        .appendChild(msg);
    };
    socket.onopen = function(event) {
      console.log('Connected:', event.target.url)
    };
    socket.onerror = function(error) {
      console.warn('WebSocket Error:' , error);
    };
  </script>
</html>

Output:

  • Jul 19 12:56:27 --- last message repeated 2 times ---
  • Jul 19 12:56:36 YourMachine.local login[30604]: USER_PROCESS: > 30604 ttys001
  • Jul 19 12:56:38 YourMachine.local login[30604]: DEAD_PROCESS: 30604 > ttys001
  • Jul 19 12:57:18 YourMachine.local login[30758]: USER_PROCESS: > 30758 ttys001
  • Jul 19 12:58:02 YourMachine kernel[0]: The USB device Apple Internal > Keyboard / Trackpad (Port 5 of Hub at 0x14000000) may have caused a > wake by issuing a remote wakeup (2)
  • Jul 19 12:58:38 --- last message repeated 1 time ---