Code Monkey home page Code Monkey logo

Comments (5)

dom96 avatar dom96 commented on May 10, 2024

What's the use case? :)

On Friday, 20 May 2016, wt [email protected] wrote:

@dom96 https://github.com/dom96

How about adding a sugar to write streaming response with "Transfer-Encoding":
"chunked" ?

import strutils, math
proc sendChunk_(response: jester.Response, data: string): Future[void] =
let dataLen = len(data)
let sizeLen = Positive(floor(log2(toFloat(dataLen)) / 4 + 1))
response.send(toHex(BiggestInt(dataLen), sizeLen) & "\r\n" & data & "\r\n")
proc endChunk_(response: jester.Response): Future[void] =
response.send("0\r\n\r\n")

Test example:

routes:
get "/":
var headers = newStringTable({
"Content-Type": "text/plain",
"Transfer-Encoding": "chunked"
})
await response.sendHeaders(Http202, headers)
await sleepAsync(1000)

await response.sendChunk("hello")
await sleepAsync(1000)

await response.sendChunk(" world")
await sleepAsync(1000)

await response.sendChunk("AAAAAAAAAAAAAAAAAAAAA")
await sleepAsync(1000)

await response.endChunk()


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#68

from jester.

tulayang avatar tulayang commented on May 10, 2024

@dom96

I am doing a hadoop service based on cloud. When user request a map reduce from my service, I will exec a hadoop mapreduce. The mapreduce will be long time, I want to tell user the real-time percentage progress:

[====  30%               ]

Then I should send a streaming response to user to update the percentage progress. The code looks like this:

proc mapreduceCb(response: Response): proc(data: JsonNode): Future[bool] =
  proc cb(data: JsonNode): Future[bool] =
    var percent = data["current"] / data["total"]
    var body = %* { "state": 0, "data": percent }
    response.sendChunk($body)
  result = cb

routes:
  post "/hadoop/mapreduce":
    let headers = newStringTable({
      "Content-Type": "application/json",
      "Transfer-Encoding": "chunked"
    })
    await response.sendHeaders(Http202, headers)

    var cluster = newHadoopCluster(...)
    var execs = await cluster.exec(..., mapreduceCb(response)) # mapreduce
    await response.endChunk()

from jester.

tulayang avatar tulayang commented on May 10, 2024

Just see ajax and web server, we can report real-time messages from the backend.

from jester.

dom96 avatar dom96 commented on May 10, 2024

I think I would prefer for this to be provided via an external library. You can easily create a jester_streaming package or similar.

from jester.

tulayang avatar tulayang commented on May 10, 2024

@dom96

Sure, that's also a good way.

This issure can be closed.

from jester.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.