DEV Community

Discussion on: HELP NEEDED: Understanding Rails ActionController::Live Module (and Async Limitations)

Collapse
 
elyalvarado profile image
Ely Alvarado

It looks like the issue is not in your broadcaster server, but in your listener server.

From the Net::HTTP docs:

By default Net::HTTP reads an entire response into memory

So you need to use response.read_body and pass a block to it, instead of response.body in your listener:

See the docs: ruby-doc.org/stdlib-2.6.5/libdoc/n...

Collapse
 
isalevine profile image
Isa Levine

Hi Ely, great call on this! response.read_body is exactly what I was missing. I really appreciate you pointing me to the right place in the docs! :)

In the end, here's the listener_controller code I used:

require 'net/http'

class ListenerController < ApplicationController
    def index
        url = URI.parse('http://localhost:3000/broadcaster')

        Net::HTTP.start(url.host, url.port) do |http|
            request = Net::HTTP::Get.new(url.to_s)
            http.request(request) do |response|

                puts <<-READOUT

                res.read_body:
                ==============================

                READOUT

                response.read_body do |data_str|
                    if data_str != ""
                        data_hash = eval(data_str.slice!(6..-1))    # slice to remove leading "data: " substring
                        char_hash = data_hash[:character]
                        Character.create("uuid": char_hash[:uuid], "name": char_hash[:name], "hp": char_hash[:hp], "magic": char_hash[:magic])
                    end
                end
            end
        end
    end
end

Thank you again!