Re: How to implement long polling?

2019-10-18 Thread filip
Yes, I can confirm it works! Thanks :)

I remember now that I tried your ws library before and got it to work. Will 
check it out again.


Re: How to implement long polling?

2019-10-18 Thread treeform
Long polling is old and awkward technology from the turn of the century. I 
recommend using web sockets instead: 
[https://github.com/treeform/ws](https://github.com/treeform/ws)

I think this fixes the issues you had: 


import asynchttpserver, asyncdispatch

var server = newAsyncHttpServer()
var requests: seq[Request]

proc returnAll() {.async.} =
  for req in requests:
await req.respond(Http200, "Hello World\n")
  requests.setLen(0)

proc cb(req: Request) {.async, gcsafe.} =
  echo req.url.path
  requests.add(req)
  if req.url.path == "/shouldReturn":
await returnAll()

waitFor server.serve(Port(8080), cb)


Run

When I just open [http://localhost:8080](http://localhost:8080)/ in my browser 
it hangs, but when I open 
[http://localhost:8080/shouldReturn](http://localhost:8080/shouldReturn) they 
all return at once.


How to implement long polling?

2019-10-18 Thread filip
How would one implement long polling in a Nim web server? Basically I would 
like to collect requests in a list and, when some event occurs, return all at 
once.

Non working pseudo code; return all when a client sends the message 
"shouldReturn": 


import asynchttpserver, asyncdispatch

var server = newAsyncHttpServer()
var requests:seq[Request]

proc returnAll() =
for req in requests:
await req.respond(Http200, "Hello World\n")
requests.clear()

proc cb(req: Request) {.async.} =
requests.add(req)
if(req.body == "shouldReturn")
returnAll()

waitFor server.serve(Port(8080), cb)


Run