Skip to content

Instantly share code, notes, and snippets.

View block1.sh
npm install --save axios
View updated_function.js
module.exports.hello = async event => {
var name = "World"
if(event.queryStringParameters != undefined && event.queryStringParameters.hello != undefined) {
name = event.queryStringParameters.hello
}
return {
statusCode: 200,
body: `Hello ${name}!`
};
};
View Response.txt
event: time
data: the current time is 2019-07-19T11:15:34.307425
event: time
data: the current time is 2019-07-19T11:15:35.307430
event: time
data: the current time is 2019-07-19T11:15:36.307562
event: time
View router.kt
router.route("/time")
.handler(TimeHandler())
.failureHandler {
it.response().end("time error asynchron response\n")
}
View TimeHandler.kt
class TimeHandler : Handler<RoutingContext> {
override fun handle(rtx: RoutingContext) {
val response = rtx.response()
response.setChunked(true)
// set headers
response.headers().add("Content-Type", "text/event-stream;charset=UTF-8")
response.headers().add("Connection", "keep-alive")
response.headers().add("Cache-Control", "no-cache")
View TimeProducer.kt
class TimeProducer {
// create singleton
companion object {
val instance = TimeProducer()
}
// create public observable
val obs = Observable.interval(1, TimeUnit.SECONDS)
.map { LocalDateTime.now() }.share()
View SseModel.kt
data class SseModel(val event: String? = null, val data: String = "", val id: String? = null, val retry: Number? = null) {
override fun toString(): String {
val sseStrings = arrayListOf<String>()
if(event != null) sseStrings.add("event: $event")
sseStrings.add("data: $data")
if(id != null) sseStrings.add("id: $id")
if(retry != null) sseStrings.add("retry: $retry")
sseStrings.add("\n")
View RestEndpoint.kt
@Controller
class RestEndpoint {
var streamResponse: CSVStreamResponseOutput
constructor(csvStreamResponseOutput: CSVStreamResponseOutput) {
this.streamResponse = csvStreamResponseOutput
}
@GetMapping(path = ["stream"], produces = [MediaType.APPLICATION_STREAM_JSON_VALUE])
View CSVStreamResponseOutput.kt
@Component
class CSVStreamResponseOutput : StreamingResponseBody {
var dataProvider: DataProvider
constructor(dataProvider: DataProvider) {
this.dataProvider = dataProvider
}
/**
View DataProvider.kt
@Component
class DataProvider {
/**
* create flowable from sampledata.csv
* @return every 100ms one line
*/
fun getDataStream(): Flowable<String> {
val csvFile = this::class.java.getResource("/static/sampledata.csv").openStream()