Sha256: f07843ddb9b98dba9b8528bd90a53c68ca426f6fd9d2ed3d62fe5081ae52758e

Contents?: true

Size: 977 Bytes

Versions: 9

Compression:

Stored size: 977 Bytes

Contents

require "logstash/outputs/base"
require "em-websocket" # rubygem 'em-websocket'

class LogStash::Outputs::Websocket < LogStash::Outputs::Base
  def initialize(url, config={}, &block)
    super
  end

  def register
    @channel = EventMachine::Channel.new
    @subscribers = 0
    host = (@url.host or "0.0.0.0")
    port = (@url.port or 3000)
    EventMachine::WebSocket.start(:host => host, :port => port) do |ws|
      ws.onopen do
        @subscribers += 1
        sid = @channel.subscribe do |msg| 
          ws.send msg
        end
        ws.onclose do
          @channel.unsubscribe(sid)
          @subscribers -= 1
        end # ws.onclose
      end # ws.onopen
    end
  end # def register

  def receive(event)
    # Only publish the event to websockets if there are subscribers
    # TODO(sissel): send a patch to eventmachine to fix this.
    if @subscribers > 0
      @channel.push event.to_json
    end
  end # def event
end # class LogStash::Outputs::Websocket

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
logstash-lite-0.2.20101124030048 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101124004656 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101123134625 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101123133737 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101120024757 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101120021802 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101119183130 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101118141920 lib/logstash/outputs/websocket.rb
logstash-lite-0.2.20101118134500 lib/logstash/outputs/websocket.rb