GithubHelp home page GithubHelp logo

happy-ferret / websocket-pipe Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattbaker/websocket-pipe

0.0 2.0 0.0 6 KB

Pipe IO to a websocket, broadcast to clients

License: Other

Ruby 67.91% Shell 3.43% HTML 28.65%

websocket-pipe's Introduction

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 ---

websocket-pipe's People

Contributors

mattbaker avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.