{"id":19541335,"url":"https://github.com/wingify/lua-resty-pubsub","last_synced_at":"2025-04-26T17:30:44.873Z","repository":{"id":49818802,"uuid":"252153528","full_name":"wingify/lua-resty-pubsub","owner":"wingify","description":"Lua Pubsub client driver for the ngx_lua based on the cosocket API","archived":false,"fork":false,"pushed_at":"2024-11-13T09:38:49.000Z","size":61,"stargazers_count":15,"open_issues_count":1,"forks_count":2,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-04T16:12:13.005Z","etag":null,"topics":["api","api-client","cosocket","hacktoberfest","lua","lua-library","luarocks","pubsub"],"latest_commit_sha":null,"homepage":"https://luarocks.org/modules/vwointegration/lua-resty-pubsub","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wingify.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-01T11:18:50.000Z","updated_at":"2025-04-03T17:26:48.000Z","dependencies_parsed_at":"2022-09-15T18:00:52.430Z","dependency_job_id":null,"html_url":"https://github.com/wingify/lua-resty-pubsub","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingify%2Flua-resty-pubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingify%2Flua-resty-pubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingify%2Flua-resty-pubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingify%2Flua-resty-pubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wingify","download_url":"https://codeload.github.com/wingify/lua-resty-pubsub/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251025567,"owners_count":21524825,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-client","cosocket","hacktoberfest","lua","lua-library","luarocks","pubsub"],"created_at":"2024-11-11T03:09:55.933Z","updated_at":"2025-04-26T17:30:44.867Z","avatar_url":"https://github.com/wingify.png","language":"Lua","readme":"# lua-resty-pubsub\n\nLua Pubsub client driver for the `ngx_lua` based on the cosocket API.\n\n[![lua module](https://img.shields.io/badge/lua-module-blue?style=for-the-badge\u0026logo=lua)](https://luarocks.org/modules/vwointegration/lua-resty-pubsub)\n[![lua module](https://img.shields.io/badge/lua%20rocks-1.4-orange?style=for-the-badge\u0026logo=lua)](https://luarocks.org/modules/vwointegration/lua-resty-pubsub)\n[![License](https://img.shields.io/badge/License-MIT-green.svg?style=for-the-badge\u0026logo=)](https://opensource.org/licenses/MIT)\n\n## Table of Contents\n\n* [Description](#description)\n* [Synopsis](#synopsis)\n* [Configs](#configs)\n* [Modules](#modules)\n    * [resty.pubsub.producer](#restypubsubproducer)\n        * [Methods](#methods)\n            * [new](#new)\n            * [send](#send)\n* [Dependencies](#dependencies)\n* [Installation](#installation)\n* [TODO](#todo)\n* [Author](#author)\n* [Copyright and License](#copyright-and-license)\n* [See Also](#see-also)\n\n## Description\n\nThis Lua library is a Pubsub client driver for the ngx_lua nginx module: http://wiki.nginx.org/HttpLuaModule\n\nThis Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior. This library pushes messages (with attributes) to Google Cloud pubsub using nginx timers and http requests.\n\nNote that at least [ngx_lua 0.9.3](https://github.com/openresty/lua-nginx-module/tags) or [ngx_openresty 1.4.3.7](http://openresty.org/#Download) is required, and unfortunately only LuaJIT supported (`--with-luajit`).\n\n## Synopsis\n\n```lua\n    lua_package_path \"/path/to/lua-resty-pubsub/lib/?.lua;;\";\n\n    server {\n        location = /publish {\n            resolver 8.8.8.8 ipv6=off;\n\n            content_by_lua_block {\n                local cjson = require \"cjson\"\n                local pubsub_producer = require \"resty.pubsub.producer\"\n                local OAUTH_TOKEN = ngx.shared.OAUTH_TOKEN -- A different dictionary can also be provided\n\n                -- A callback which will recieve messages if gets successfully sent\n                -- Types of messages \u0026 err are a table\n                local success_callback = function (topic, err, messages)\n                    ngx.log(ngx.INFO, \"Messages: \", cjson.encode(messages), \" successfully pushed to topic: \", topic)\n                end\n\n                -- A callback which will recieve messages if gets failed\n                -- Types of messages \u0026 err are a table\n                local error_callback = function (topic, err, messages)\n                    for _, message in ipairs(messages) do\n                        ngx.log(ngx.ERR, \"Failed to send Message : \", cjson.encode(message), \" with err: \", cjson.encode(err))\n                    end\n                end\n\n                local publish = function()\n\n                    -- Pubsub Producer Config\n                    local pubsub_config = {\n                        project_id = \"demo-project\",\n                        topic = \"demo-topic\",\n                        pubsub_base_domain = \"pubsub.googleapis.com\",\n                        pubsub_base_port = 443,\n                        is_emulator = false,\n                        producer_config = {\n                            max_batch_size = 200, -- number of packets\n                            max_buffering = 5000, -- max number of packets in buffer\n                            timer_interval = 10000, -- in milliseconds\n                            last_flush_interval = 5000, -- in milliseconds\n                            http_timeout = 6000, -- in milliseconds\n                            keepalive_max_idle_timeout = 2000, -- in milliseconds\n                            keepalive_pool_size = 50\n                        },\n                        oauth_config = {\n                            service_account_key_path = \"/etc/key.json\", -- Replace this with your own key path\n                            oauth_base_uri = \"https://www.googleapis.com/oauth2/v4/token\",\n                            oauth_scopes = {\n                                \"https://www.googleapis.com/auth/pubsub\"\n                            },\n                            oauth_token_dict = OAUTH_TOKEN\n                        },\n                        success_callback = success_callback,\n                        error_callback = error_callback\n                    }\n\n                    -- Create the producer object\n                    -- No matter how many times you call new, the producer instance will always be generated once per topic per worker process\n                    local producer, err = pubsub_producer:new(pubsub_config)\n\n                    -- Also check if there is any error while initializing producer\n                    if err ~= nil then\n                        ngx.log(ngx.ERR, \"Unable to create pubsub producer \", err)\n                        return\n                    end\n\n                    -- Finally send the message with attributes.\n                    local ok, send_err = producer:send(\"Some Random Text\", {\n                        attr1 = \"Test1\",\n                        attr2 = \"Test2\"\n                    }, \"optional_ordering_key\")\n\n                    -- Also check if there is any error while sending message\n                    if send_err ~= nil then\n                        ngx.log(ngx.ERR, \"Unable to send data to pubsub: \", send_err)\n                        return\n                    end\n\n                end\n\n                -- Publish Message\n                publish()\n            }\n\n        }\n    }\n```\n\n## Configs\n\n### Producer Configs\n\n| Property | Data Type | Description | Default Value |\n| :---: | :---: | :---: | :---: |\n| project_id | string | Specifies the project id as a string of your Pub/Sub project | none (Required) |\n| topic | string | Specifies the topic in which the data needs to be send | none (Required) |\n| pubsub_base_domain | string | Specifies the base domain through which the http connection is made. | pubsub.googleapis.com |\n| pubsub_base_port | number | Specifies the base domain port through which the http connection is made. | 443 |\n| is_emulator | boolean |  Specifies a boolean value. true if you are communicating with. | false |\n| producer_config.max_batch_size | number | Specifies the max batch size that will be pushed to pubsub. | 200 |\n| producer_config.max_buffering | number | Specifies the max size of the buffer which will hold the data for a specific duration of time. | 5000 |\n| producer_config.timer_interval | number (milliseconds) | Specifies the time interval in which the stale messages in buffer are checked for publishing. | 10000 |\n| producer_config.last_flush_interval | number (milliseconds) | Specifies the max interval between the last flush time and current time. Used when packets in buffer are less than the batch size for a longer period of time. | 10000 |\n| producer_config.http_timeout | number (milliseconds) | Sets the timeout protection for subsequent operations, including the connect method. | 5000 |\n| producer_config.keepalive_max_idle_timeout | number (milliseconds) | Used in httpc:set_keepalive which attempts to puts the current connection into the ngx_lua cosocket connection pool. | 2000 |\n| producer_config.keepalive_pool_size | number | Used in httpc:set_keepalive which attempts to puts the current connection into the ngx_lua cosocket connection pool. | 50 |\n| oauth_config.service_account_key_path | string | Specifies the path for service account key that are used to authenticate to pub/sub project. | none (Required) |\n| oauth_config.oauth_base_uri | string | Specifies the base uri to which request is made to Google authorization server for a token that will be used in subsequent requests. | https://www.googleapis.com/oauth2/v4/token |\n| oauth_config.oauth_scopes | list of string | Specifies a table comprising of OAuth 2.0 scopes that you might need to request to access Google APIs, depending on the level of access you need. | {\"https://www.googleapis.com/auth/pubsub\"} |\n| oauth_config.oauth_token_dict | lua_shared_dict | Specifies a shared memory zone across workers, to serve as a storage for the oauth token. | ngx.shared.OAUTH_TOKEN |\n| success_handler | function | This is a callback function which will be provided with all the messages with their attributes which are successfully pushed to pubsub. | none (Optional) |\n| error_handler | function | This is a callback function which will be executed when a batch fails. | none (Optional) |\n\n[Back to TOC](#table-of-contents)\n\n## Modules\n\n### resty.pubsub.producer\n\nTo load this module, just do this\n\n```lua\n    local producer = require \"resty.pubsub.producer\"\n```\n\n[Back to TOC](#table-of-contents)\n\n### Methods\n\n#### new\n\n`syntax: local p, err = producer:new(pubsub_config)`\n\n#### send\n`syntax: p:send(message, attributes[, ordering_key])`\n\n* Requires a message of type string, attributes of type table and an optional ordering_key of type string\n\n[Back to TOC](#table-of-contents)\n\n## Dependencies\n\nLua modules required to build this library are:\n\n* [lua-cjson](https://github.com/openresty/lua-cjson.git) library\n* [lua-resty-rsa](https://github.com/spacewander/lua-resty-rsa) library\n* [lua-resty-http](https://github.com/ledgetech/lua-resty-http) library\n\n## Installation\n\n### For Installing module\n\n#### Method 1 - Using luarocks\n\nFor installing luarocks refer [this](https://github.com/luarocks/luarocks/wiki/Download)\n\nYou can simply run `luarocks install lua-resty-pubsub` for installing this library\n\n#### Method 2 - Using Makefile\n\nYou need to configure\nthe lua_package_path directive to add the path of your lua-resty-pubsub source\ntree to ngx_lua's LUA_PATH search path, as in\n\n```nginx\n    # nginx.conf\n    http {\n        ..\n        lua_shared_dict     OAUTH_TOKEN 1m; # Replace OAUTH_TOKEN with the name you mentioned in `oauth_token_dict`\n        ..\n        lua_package_path \"/path/to/lua-resty-pubsub/lib/?.lua;;\";\n        ...\n    }\n```\n\nYou also need to add google resolver so as to connect with pubsub endpoints\n```nginx\n    # default.conf\n    server {\n        location = /test {\n            resolver 8.8.8.8 ipv6=off;\n            ...\n\t    }\n    }\n```\n\nEnsure that the system account running your Nginx ''worker'' proceses have\nenough permission to read the `.lua` file.\n\nAlso if using OAUTH_TOKEN dictionary, add `lua_shared_dict` in nginx.conf\n\nFinally run `make install` for installing the module\n\n### For Running Test Cases\n\nInstall [Test::Nginx](https://github.com/openresty/test-nginx) module for running test cases\n\nFinally run `make test` for running the test cases\n\n[Back to TOC](#table-of-contents)\n\n## TODO\n\n1.  Pubsub Message Fetch API\n2.  Add Unit Test Cases\n\n\n[Back to TOC](#table-of-contents)\n\n## Author\n\nVasu Gupta (vasu7052) \u003chttps://github.com/Vasu7052\u003e.\n\n\n[Back to TOC](#table-of-contents)\n\n## Copyright and License\n\n\u003e The MIT License (MIT)\n\u003e\n\u003e Copyright (c) 2020 Wingify Software Pvt. Ltd.\n\u003e\n\u003e Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\u003e\n\u003e The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\u003e\n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n\n[Back to TOC](#table-of-contents)\n\n## See Also\n\n* the ngx_lua module: http://wiki.nginx.org/HttpLuaModule\n\n[Back to TOC](#table-of-contents)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwingify%2Flua-resty-pubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwingify%2Flua-resty-pubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwingify%2Flua-resty-pubsub/lists"}