Hello,

I am in the midst of building a game server with Luvit, because I would 
like to script the backend in Lua.  I've been pointed toward OpenResty as 
another option.  I'm very curious about OpenResty as it seems to be very 
active.  I've been reading through the docs and examples, and I can't seem 
to fully wrap my head around how a socket server would work with OpenResty 
being HTTP based on nginx.

    You could connect initially via HTTP and Upgrade to a web socket connection maybe?
      Hello!
      
      On Tue, Jan 7, 2014 at 7:00 PM, develephant.net wrote:
      > I've been reading through the docs and examples, and I can't seem to fully
      > wrap my head around how a socket server would work with OpenResty being HTTP
      > based on nginx.
      >
      
      Yes, OpenResty has been focusing on HTTP. But there has been a plan to
      add support for pure TCP downstream. bigplum in the community has done
      some attempts like this:
      
          https://github.com/bigplum/nginx-tcp-lua-module
      
      And I'm going to integrate such a ngx_tcp_lua module into OpenResty soon.
      
      In the meantime, you can use websockets as a work-around, just as
      Brian Akins has pointed out :)
      
      > From what I have seen so far I assume something like this:
      >
      [...]
      >
      > Now I know that's not a realistic app, but I am wondering if that's the
      > general direction.
      
      Yes, it is the supposed way of running the server. It is also demonstrated here:
      
          http://openresty.org/#Samples
      
      >  Most importantly, how does this work in a non web based
      > environment?  I would be connecting with luasocket from a Lua mobile client
      > to the OpenResty app instance, not through a web client.
      
      If you mean using OpenResty to write a standalone console-based client
      program, then we'll have a "ngxlua" utility program in OpenResty soon.
      You'll be able to run non-server programs like this:
      
          $ ngxlua myclient.lua
      
      And no ports and no socket files will be listened.
      
      > I also would like
      > to keep all functionality in external Lua files as opposed to the config
      > file.
      >
      
      For servers not based on the upcoming "ngxlua" utility mentioned
      above, it requires and will still require a configuration file. You
      can have a fixed configuration file for your app, just like
      PHP/Python/Perl/Ruby web apps often have a relatively fixed nginx
      configuration file. We can add support for pure Lua configuration
      files to nginx in the future. But it is still a configuration file.
      
      Best regards,
      -agentzh
      
        Hi agentzh,

        Thank you for taking the time to answer my questions.  And thank you Brian as well.

        My goal is to connect a Corona SDK ( Lua ) client, which has luasocket built-in to an OpenResty instance and create a full-duplex socket connection.  Can this be done with websockets?

        Also, I was unclear about the config file.  In regards to using only lua files, I see that this can be done with the content_by_lua_file, so that's perfect. :)

        I'll take a look at the nginx-tcp-lua-module as well.

        Thank you so much for your help.  I'm excited to start working with OpenResty.

        Best.












        On Wed, Jan 8, 2014 at 3:11 PM, Yichun Zhang (agentzh) <age...@gmail.com> wrote:
        Hello!

        On Tue, Jan 7, 2014 at 7:00 PM, develephant.net wrote:
        > I've been reading through the docs and examples, and I can't seem to fully
        > wrap my head around how a socket server would work with OpenResty being HTTP
        > based on nginx.
        >

        Yes, OpenResty has been focusing on HTTP. But there has been a plan to
        add support for pure TCP downstream. bigplum in the community has done
        some attempts like this:

            https://github.com/bigplum/nginx-tcp-lua-module

        And I'm going to integrate such a ngx_tcp_lua module into OpenResty soon.

        In the meantime, you can use websockets as a work-around, just as
        Brian Akins has pointed out :)

        > From what I have seen so far I assume something like this:
        >
        [...]
        >
        > Now I know that's not a realistic app, but I am wondering if that's the
        > general direction.

        Yes, it is the supposed way of running the server. It is also demonstrated here:

            http://openresty.org/#Samples

        >  Most importantly, how does this work in a non web based
        > environment?  I would be connecting with luasocket from a Lua mobile client
        > to the OpenResty app instance, not through a web client.

        If you mean using OpenResty to write a standalone console-based client
        program, then we'll have a "ngxlua" utility program in OpenResty soon.
        You'll be able to run non-server programs like this:

            $ ngxlua myclient.lua

        And no ports and no socket files will be listened.

        > I also would like
        > to keep all functionality in external Lua files as opposed to the config
        > file.
        >

        For servers not based on the upcoming "ngxlua" utility mentioned
        above, it requires and will still require a configuration file. You
        can have a fixed configuration file for your app, just like
        PHP/Python/Perl/Ruby web apps often have a relatively fixed nginx
        configuration file. We can add support for pure Lua configuration
        files to nginx in the future. But it is still a configuration file.

        Best regards,
        -agentzh
        .

          Hello!
          
          On Wed, Jan 8, 2014 at 1:27 PM, C Byerley wrote:
          >
          > My goal is to connect a Corona SDK ( Lua ) client, which has luasocket
          > built-in to an OpenResty instance and create a full-duplex socket
          > connection.  Can this be done with websockets?
          >
          
          WebSocket is a full duplex protocol. All you need is a websocket
          framing support in both the client and the server. The
          lua-resty-websocket library provides the support in OpenResty:
          
              https://github.com/agentzh/lua-resty-websocket
          
          > Also, I was unclear about the config file.  In regards to using only lua
          > files, I see that this can be done with the content_by_lua_file, so that's
          > perfect. :)
          >
          
          Right. content_by_lua_file is all you need.
          
          Regards,
          -agentzh
          
            10 years later

            Thank you for sharing this valuable information on OpenResty. Your detailed explanation and examples make it much easier for developers like me to understand and implement. Keep up the excellent work!

            Best regards,
            TechToDown

              Write a Reply...