Skip to content

Instantly share code, notes, and snippets.

@ailabs-software
Created October 13, 2025 01:26
Show Gist options
  • Select an option

  • Save ailabs-software/fb72cf67ffa5d4c9ec0b004230ee23f3 to your computer and use it in GitHub Desktop.

Select an option

Save ailabs-software/fb72cf67ffa5d4c9ec0b004230ee23f3 to your computer and use it in GitHub Desktop.
const url = 'http://localhost:8080';
fetch(url)
.then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder();
function readChunk() {
reader.read().then(({done, value}) => {
if (done) {
console.log('Stream closed');
return;
}
// Print each chunk as it arrives
const text = decoder.decode(value);
console.log(text);
readChunk();
});
}
readChunk();
});
import 'dart:async';
import 'dart:io';
Future<void> main() async {
final server = await HttpServer.bind(InternetAddress.anyIPv4, 8080);
print('Listening on localhost:${server.port}');
await for (HttpRequest request in server) {
// Set the response to chunked mode
request.response
..headers.contentType = ContentType.text
..headers.set('Cache-Control', 'no-cache')
..headers.set('Transfer-Encoding', 'chunked');
// Send a chunk of text every second
Timer.periodic(Duration(seconds: 1), (timer) {
final message = 'Chunk at ${DateTime.now()}\n';
request.response.write(message);
// Flush to send data immediately
request.response.flush();
// Stop after 10 chunks for this example
if (timer.tick >= 10) {
request.response.close();
timer.cancel();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment