long-polling


Koa 2 router how to make a long poll request


I want to make a long pooling request. So I want to take a request and send a response after some delay. Is it possible?
I'm trying to use the async/await synax but it's isn't working for me (I got an error 404 on the client)
many thanks for any help.
Here is my server
import 'babel-polyfill';
import Koa from 'koa';
import Router from "koa-router";
import fs from "fs";
const router = new Router();
const convert = require('koa-convert')
const serve = require("koa-static");
const app = new Koa();
router
.get('/*', async function (ctx, next) {
ctx.response.type = 'text/html; charset=utf-8';
/* await (() => {
setTimeout( () => {ctx.body = fs.readFileSync(__dirname + "/public/index.html")}, 1000)
})(); */
//ctx.body = fs.readFileSync(__dirname + "/public/index.html");
})
app.use(convert(serve(`${__dirname}/public`)))
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
Generally, yes. This is possible.
The problem in your code is, that await is entierly based on promises. So your timeout function needs to be encapsulated into a promise. Something like this could work:
...
function delayed(ctx, ms) {
return new Promise((resolve, reject) => {
setTimeout(function() {
ctx.body = fs.readFileSync(__dirname + "/public/index.html")
resolve();
}, ms);
})
}
router.get('/*', async function (ctx, next) {
ctx.response.type = 'text/html; charset=utf-8';
await delayed(ctx, 1000);
})
...

Related Links

Koa 2 router how to make a long poll request
Confluent long polling properties in kafka-rest
How to combine async and blocking requests in VuGen?
Long polling confusion
WAS Liberty & Atmosphere long-polling 1 min delay
Long Polling interval in atmosphere
MQTT long polling over GPRS/HSDPA
JS and CSS file fails to load when the page is refreshed in grails application which uses Atmosphere Meteor plugin
Security concerns in long polling
Kafka long polling
How to build the request for a long poll url for box.com
how to access enterprise events using Java code
Handle client timeouts when serving long polling with gevent
Long polling blocking multiple windows?
Long polling on a penny auction site?
Cannot implement long polling

Categories

HOME
yii2
vbscript
push-notification
fft
office365api
webstorm
google-docs
razor
yarn
malloc
serverspec
enterprise-library-5
podio
synchronization
gitpitch
session-variables
resize
jplayer
commonmark
fallback
quartz-scheduler
jsdoc
reactcsstransitiongroup
reactive-cocoa
ab-testing
nodatime
plunker
abi
csrf-protection
tapestry
zurb-foundation-6
apache-commons-io
status
facebook-access-token
gitignore
tasklet
bpel
librato
instant-messaging
kendo-ui-grid
lxd
hybridauth
textmate
ios5
adobe-premiere
bitbucket-pipelines
swift3.0.2
objectlistview
unspecified
reportbuilder
sqlite2
temporary-files
elasticsearch-plugin
email-templates
fedex
ncalc
turbogears
vao
datastax-startup
domain-model
boost-preprocessor
sqldf
jspdf-autotable
hittest
vtigercrm
xcb
color-picker
httplistener
sts-springsourcetoolsuite
file-writing
ctest
contact-list
fputcsv
whois
associative-array
system.management
hsv
arcanist
oberon
flask-cors
aapt
elliptic-curve
funcunit
cloud-connect
terminal-services
flexmojos
farseer
runtime.exec
ocunit
selected
e4x
simba
chuck
bitsharp
funscript
first-responder
objective-c-2.0
boost-filesystem
horizontal-accordion
locationlistener
self-extracting
actionview
h.323
createwindow
gamma
xsdobjectgen
user-friendly
xetex
yagni
scripting-languages
mdac
django-notification
ntvdm.exe

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App