mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
- Only push events to websockets when we have subscribers
This commit is contained in:
parent
97d71ed130
commit
8ef912a624
1 changed files with 7 additions and 1 deletions
|
@ -8,21 +8,27 @@ class LogStash::Outputs::Websocket < LogStash::Outputs::Base
|
|||
|
||||
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)
|
||||
@channel.push event.to_json
|
||||
# Only publish the event to websockets if there are subscribers
|
||||
if @subscribers > 0
|
||||
@channel.push event.to_json
|
||||
end
|
||||
end # def event
|
||||
end # class LogStash::Outputs::Websocket
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue