pause and resume from the server side?
# javalin
t
pause and resume from the server side?
a
Yes
Basically this in javalin
t
🤔
you shouldn't have to keep any state on the server
if you want to make an audio player that can play/pause/seek(skip), all you have to do on the server is to give
seekableStream()
the inputstream to your file, the client side takes care of the rest
a
what does the client side need to do though? Lets say client A joins at 1pm and client B joins at 1:02pm - how do I make sure both of them are listening to the same audio timestamp? [i.e. client A is not listening to a later part of the audio compared to client B]
t
aha, you want to make a radio
i thought you were just serving files
a
Yes yes
t
i've never made a radio before, but that's an interesting question
a
It looks like the vertx one checks if the ‘writeQueue’ is full and then for each connected client sends a chunk of the buffer to a chunked http response
Not sure what the support for chunked responses are in javalin
t
should be fine, jetty takes care of that
it does already chunk static files
chunked makes more sense than ranged to me for something like a radio
a
What is tge difference between range and chunked?
Is range a way to send large files without killing memory?
t
for chunked you send a "chunk" of data to the client, and it doesn't know if it's the last piece or not
for ranged the client needs to make subsequent requests with byte-ranges set
like "give me range 0-200_000", give me range "200_000-400_000"
a
Do u have a guide on the client side how it does that? Like is that in the header?
t
it's a way to send just what the client needs, for example if you open a MP3 and skip to the second half
yeah, it's managed automatically by for example the
<audio>
tag in html
a
How do i send chunked byte/buffer responses?
t
setting the header should be sufficient
a
But how do i send the chunks though - ctx.result overwrites result
Like if i loop to 5 and write the index - i only get 4 in the final response
t
you'll have to write to the outputstream directly
a
Yes - i tried using the print writer - but it does not have a write bytes
Only char arrays/strings and ints
t
ctx.res.outputStream
?
a
Oh
I was using it.res.writer (print writer)
One sec let me try that
Copy code
app.get("/") {
    it.res.setHeader("Transfer-Encoding", "chunked")
    it.contentType("text/plain")
    repeat(5) { c ->
      it.res.outputStream.write(c)
    }
    it.res.outputStream.flush()
    it.status(200)
  }
  app.start()
hmm 🤔 this just downloads an empty 5 byte file when i hit localhost:7000 in chrome
t
it looks like you write five times nothing to it
what is c ?
a
C is the int index in the repeat loop
It.res.outputstream.write
does that not write the index of the repeat loop
And then i flush at the end after the loop