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

Streaming content every X seconds #250

Closed
jaume-pinyol opened this Issue Sep 17, 2015 · 3 comments

Comments

3 participants
@jaume-pinyol

jaume-pinyol commented Sep 17, 2015

Hello,

I don't know if it's the place to ask but I need to have a endpoint that streams data every 2 seconds without closing the connection and in different chunks. I'm struggling a little to find a solution for this case.

Is it posible to stream self-generated data in different chunks with Finatra?

Best Regards,
Jaume Pinyol

@scosenza

This comment has been minimized.

Contributor

scosenza commented Sep 18, 2015

Hi Jaume,

You have two options for streaming HTTP responses:

  • Return an AsyncStream
  • Return a finagle-httpx Response with setChunked(true)

The following controller demonstrates some streaming HTTP responses:
https://github.com/twitter/finatra/blob/master/http/src/test/scala/com/twitter/finatra/http/integration/tweetexample/main/controllers/TweetsController.scala

See also Future#sleep to wait without blocking.

On Thu, Sep 17, 2015 at 8:25 AM, Jaume Pinyol Vilardell <
notifications@github.com> wrote:

Hello,

I don't know if it's the place to ask but I need to have a endpoint that
streams data every 2 seconds without closing the connection. And I'm
struggling to find a solution for this case.

Is it posible to stream self-generated data with Finatra?

Best Regards,
Jaume Pinyol


Reply to this email directly or view it on GitHub
#250.

@jaume-pinyol

This comment has been minimized.

jaume-pinyol commented Sep 18, 2015

Hi @scosenza,

Thank you very much for you're help.

I almost have it, but the only problem is that if I use AsyncStream it wraps the requests with []

what I need to sent is

data: {some content}

data:{some other content}

but with AsyncStream it wraps the content with

[[
data: {some content},
data:{some other content}
]]

Which using Streaming response, I get the expected output but I don't see how to add headers to the response (I guess I'm too new at finatra, or scala)

get("/test.stream") { request: Request =>
    StreamingResponse(Buf.Utf8.apply) {
      streamContent(1)
    }
  }

I've also tried with setChunked to true, hoping for more flexibility

get("/tweets/streaming_manual_writes") { request: Request =>
    val response = Response()
    response.setChunked(true)

    response.writer.write(Buf.Utf8("hello")) before {
      response.writer.write(Buf.Utf8("world")) ensure {
        response.close()
      }
    }
    Future(response)
  }

But I always get and error message saying:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.twitter.finagle.httpx.Request, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information

@jaume-pinyol

This comment has been minimized.

jaume-pinyol commented Sep 19, 2015

Hi @scosenza,

I've already solved the problem, I ended up modifying the StreamingResponse class to allow custom headers and onFailure and onCompleted callbacks.

I'll do a PR with the changes, maybe someone is interested too

Thank you very much for the help!

@cacoco cacoco closed this Oct 1, 2015

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