Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flush method in HttpServer::Response doesn't seem to work #7722

Closed
unplugandplay opened this issue Apr 26, 2019 · 4 comments
Closed

flush method in HttpServer::Response doesn't seem to work #7722

unplugandplay opened this issue Apr 26, 2019 · 4 comments

Comments

@unplugandplay
Copy link

unplugandplay commented Apr 26, 2019

Hi, I 'm trying to replicate the Server Side Event example from https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events , using this code :

require "http/server"

port = 5000

server = HTTP::Server.new do |context|
  puts "Got request"
  # `context.response.headers["Transfer-Encoding"] = "chunked"` is default.
   context.response.headers["Content-Type"] = "text/event-stream"
  context.response.headers["Access-Control-Allow-Origin"] = "*"
  context.response.headers["Access-Control-Allow-Methods"] = "GET, HEAD, POST, PUT"
  context.response.headers["Access-Control-Allow-Headers"] = "Content-Type, Accept, Origin, Authorization"
  context.response.headers["Access-Control-Max-Age"] = "86400"
  loop do
    context.response.puts "event: ping\n";
    context.response.puts "data: hello\n"
    context.response.flush
    puts "Writing"
    sleep 5
    context.response.puts "data: world\n"
    context.response.flush
    puts "Writing"
  end
end

address = server.bind_tcp 5000
puts "Listening on http://#{address}"
server.listen

I was hoping that


would do a similar job as

ob_end_flush();
  flush()

in the PHP example part, I mean, sending (flushing) response while keeping the http connexion open

Am I missing something or is the flush method just not design to do that ?

crystal --version
Crystal 0.27.2 (2019-02-05)

LLVM: 6.0.1
Default target: x86_64-apple-macosx
@vladfaust
Copy link
Contributor

I'm not much sure, but it might be a good idea to debug the request processor, adding pp as breakpoints to the HTTP::Server::RequestProcessor#process method to find out what's going on...

@straight-shoota
Copy link
Member

Yes, #flush should write the buffer to the IO which means currently queued data should be send.

Unfortunately, your post lacks sufficient information to be able to help you. Could you provide a full description how you're actually testing this server code, what you expect it to do and what exactly goes wrong?

@straight-shoota
Copy link
Member

@vladfaust Your suggestion is not very helpful. It's based on a wild guess that RequestProcessor#process might be faulted, which is almost impossible to be relevant here. If there is actually a bug somewhere, it's in the IO stack. But without any information about how the server is supposed to behave and what it actually does, there is no way to tell what's actually the issue here.

@unplugandplay
Copy link
Author

In fact it was me, as this code is perfectly working with the EventSource javascript part :

  3.times do |i_times|
    #context.response.puts "id: #{i_times+1}\n"
    #context.response.puts "event: ping\n"
    context.response.puts "data: hello"
    context.response.puts "data: world"
    context.response.puts "\n\n"
    puts "Writing"
    context.response.flush
    sleep 1
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants