Class: LogStash::Outputs::Websocket

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/websocket.rb

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#receive(event) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/logstash/outputs/websocket.rb', line 29

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
    @logger.info("Sending event to websocket.")
    @channel.push event.to_json
  end
end

#registerObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/logstash/outputs/websocket.rb', line 7

def register
  @channel = EventMachine::Channel.new
  @subscribers = 0
  @url.host = (@url.host or "0.0.0.0")
  @url.port = (@url.port or 3232)
  @logger.info("Registering websocket on #{@url}")
  EventMachine::WebSocket.start(:host => @url.host, :port => @url.port) do |ws|
    ws.onopen do
      @subscribers += 1
      @logger.info("New #{self.class.name} connection")
      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