leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.12k stars 247 forks source link

Websocket example/support? #233

Open misha opened 9 years ago

misha commented 9 years ago

I'm exploring the Lapis/OpenResty/Nginx stack for personal project. I noticed that OpenResty has support for websockets; is it possible to reach through Lapis, or integrate websockets somehow? I looked around GitHub and the Lapis reference manual but I didn't find anything.

Examples/documentation/thoughts are welcome.

lordnynex commented 9 years ago

@msoliter Lapis does not currently support websockets. I've been thinking about this lately for some lapis projects of mine and I think it would be awesome to hear @leafo's thoughts on including support for it. It's a non-trivial change though because of the way openresty/lua-resty-websocket works. As a hack you can implement your websocket loop in another location {} directive and use ngx.location.capture() to pass messages back and forth to lapis. I would not recommend this hack for a heavy traffic scenario.

@leafo if websockets are on the road map (it would be exciting if it was) and you're overhauling the dispatcher I think it would be awesome to have an alternative to serve() which works in the ACCESS phase to bypass the output buffer. Aside from being able to check session state outside a before_filter, I would be able to do basic request validation before passing the request onto the content phase. I've seen a fair bit of requests for lapis in the access phase and I'm curious what your thoughts are on this.

bdjnk commented 9 years ago

The integration of websockets into Lapis is a feature that interests me as well. It would be good to get input from @leafo on the possibility.

revskill10 commented 9 years ago

There is a websocket module there. Is it production ready ?

nonchip commented 9 years ago

@checkraiser where is there?

jots commented 8 years ago

+1 for websockets in lapis.

aiibe commented 6 years ago

+1 websockets supported in Lapis

chelog commented 6 years ago

Would be a great thing to see websockets in Lapis as we're using it in our community for almost every web thingie

+1 here

nonchip commented 6 years ago

interesting to see how there isn't even a simple statement on this feature request for 3 years while implementing websocket support in other non-monolithic-as-hell frameworks that don't even support it yet takes about half an hour of trial and error... seems like @leafo doesn't even care enough anymore to talk to us if it can't sell us an itch.io feature...

ghost commented 5 years ago

Not having websockets might be a show-stopper. It's 2019 ffs, get it together.

nonchip commented 5 years ago

@sci4me it's definitely possible using the openresty ecosystem, see also how i did it here using my own framework instead: https://gitlab.com/nonchip/lite-roller.nonchip.de/blob/master/apps/socket.moon

there's just no utilities in lapis for it yet. but if you manage to circumvent enough of lapis' HTTP and output handling code to be able to call resty.websocket.server\new without it interfering it should work.

ryanford commented 5 years ago

Verbal +1 and "me toos" contribute nothing to the thread. If you want to show your support add a :+1: in the appropriate place.

eko234 commented 3 years ago

So, ws when?

nonchip commented 3 years ago

@eko234 judging by any other feature ever requested and then possibly implemented since @leafo launched itch.io: when that site needs it.

Juniiorf commented 1 year ago
AdamLearns commented 4 months ago

There's some more context on the difficulty behind this feature in this Hacker News comment from @leafo.

leafo commented 2 months ago

Just want to note that if you are in Openresty you can use resty.websocket with no issues to "upgrade" the request to a websocket within the action handler. In this example, data is streamed over a websocket to the client over 10 seconds.

Things to note:

app:match("/websocket-test", function(self)
  local ws = require "resty.websocket.server"
  local server = assert(ws:new())
  for i = 1, 10 do
    if not server:send_text("hello world: " .. i) then
      -- if sending fails, collection is probably closed
      break
    end
    ngx.sleep(1)
  end
  server:send_close()
  return { skip_render =  true }
end)