Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails how to parse text/event-stream?

I have an API url that is a stream of data with the content type: text/event-stream.

How is it possible to listen to the stream? Like subsribe to each event to print the data? I have tried to use the ruby libary em-eventsource

My test.rb file:

require "em-eventsource"
EM.run do
  source = EventMachine::EventSource.new("my_api_url_goes_here")
  source.message do |message|
    puts "new message #{message}"
  end
  source.start 
end

When I visit my api url I can see the data updated each second. But when I run the ruby file in the terminal it does not print any data/messages.

like image 384
Rails beginner Avatar asked Nov 22 '15 10:11

Rails beginner


2 Answers

Set a timer to check source.ready_state, it seems like it does not connect to api for some reason

EDIT: it seems your problem is in https' SNI, which is not supported by current eventmachine release, so upon connecting eventsource tries to connect to default virtual host on api machine, not the one the api is on, thus the CONNECTING state

Try using eventmachine from master branch, it already states to have support for SNI, that is going to be released in 1.2.0:

gem 'eventmachine', github:'eventmachine/eventmachine'
like image 95
Vasfed Avatar answered Oct 11 '22 20:10

Vasfed


require 'eventmachine'
require 'em-http'
require 'json'

http = EM::HttpRequest.new("api_url", :keepalive => true, :connect_timeout => 0, :inactivity_timeout => 0)

EventMachine.run do

  s = http.get({'accept' => 'application/json'})
  s.stream do |data|
    puts data
  end

end

I used EventMachine http libary.

like image 28
Rails beginner Avatar answered Oct 11 '22 20:10

Rails beginner