Sha256: e84d5d7aac79f8c05f8fa4da475d36ee4fc3cc9690223021c7802f26c3d87b85

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8
require "logstash/namespace"
require "logstash/outputs/base"


# This output runs a websocket server and publishes any 
# messages to all connected websocket clients.
#
# You can connect to it with ws://<host\>:<port\>/
#
# If no clients are connected, any messages received are ignored.
class LogStash::Outputs::WebSocket < LogStash::Outputs::Base
  config_name "websocket_topics"

  # The address to serve websocket data from
  config :host, :validate => :string, :default => "0.0.0.0"

  # The port to serve websocket data from
  config :port, :validate => :number, :default => 3232

  def make_pubsub(topic)
      pubsub = Logstash::Outputs::WebSocket::Pubsub.new
      pubsub.logger = @logger
  end

  public
  def register
    require "ftw"
    require "logstash/outputs/websocket/app"
    require "logstash/outputs/websocket/pubsub"
    @channels = {}
    @server = Thread.new(@channels) do |channels|
      begin
        Rack::Handler::FTW.run(LogStash::Outputs::WebSocket::App.new(channels, @logger),
                               :Host => @host, :Port => @port)
      rescue => e
        @logger.error("websocket server failed", :exception => e)
        sleep 1
        retry
      end
    end
  end # def register

  public
  def receive(event)
    topic = event['topic']
    if @channels.has_key?(topic) 
      @channels[topic].publish(event.to_json)
    else
      pubsub = make_pubsub(topic) 
      @channels[topic] = pubsub
      pubsub.publish(event.to_json)
    end # if
  end # def receive

end # class LogStash::Outputs::Websocket

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-output-websocket_topics-2.1.8 lib/logstash/outputs/websocket_topics.rb