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

Streaming content every X seconds #250

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

Streaming content every X seconds #250

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

Comments

@jaume-pinyol
Copy link

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
Copy link
Contributor

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 <
[email protected]> 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
Copy link
Author

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
Copy link
Author

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 as completed Oct 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants