Skip to content

Commit 5adc79f

Browse files
committed
СТРУКТУРИЗАЦИЯЯЯЯЯ
1 parent e9c708f commit 5adc79f

6 files changed

Lines changed: 101 additions & 62 deletions

File tree

README.MD

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
<!-- personal notes
2-
3-
-------
4-
5-
-->
6-
71
# Long Polling Server
82

93
A simple [long-polling](https://javascript.info/long-polling) server (and client) written in Lua.
@@ -12,15 +6,6 @@ A simple [long-polling](https://javascript.info/long-polling) server (and client
126

137
_source: javascript.info_
148

15-
## Features:
16-
17-
- Can be used to deliver messages to multiple clients at the same time.
18-
- Tested in a production environment
19-
- docker ready
20-
- Contains Client and Server components. Also contains advanced application example
21-
- You can specify predefined parameters to be added to each update object, which is useful when you want to recognize different services that send updates to the same link.
22-
- Ability to customize the amount of data stored and the storage time (for redis) so that no garbage is stored
23-
249
## Use cases:
2510

2611
- In environments where it is not possible to create a web server (e.g. in a Garry's Mod game), but getting "webhooks" is necessary
@@ -30,36 +15,41 @@ _source: javascript.info_
3015
## Project structure
3116

3217
```
33-
.
34-
├──  lua
35-
──  long-polling
36-
├──  application
37-
│ ├── docker-compose.yml
38-
│ ├── Dockerfile
39-
│ ├──  init.lua
40-
│ └──  README.MD
41-
──  client
42-
├──  init.lua
43-
└──  README.MD
44-
│ └──  server
45-
──  dataproviders
46-
├──  localtable.lua
47-
──  redis.lua
48-
──  misc
49-
├──  pubsub.lua
50-
──  redis-safe.lua
51-
──  init.lua
52-
└──  README.MD
53-
├── LICENSE
54-
└── README.MD
18+
.
19+
├── examples
20+
── advanced
21+
├── client.lua
22+
│ │ ├── docker-compose.yml
23+
│ │ ├── Dockerfile
24+
│ │ ├── README.MD
25+
│ │ └── server.lua
26+
── simple
27+
│ ├── README.MD
28+
│ └── server.lua
29+
── lua
30+
── long-polling
31+
│ ├── dataproviders
32+
│ │ ── localtable.lua
33+
── redis.lua
34+
│ ├── misc
35+
│ │ ── pubsub.lua
36+
── redis-safe.lua
37+
│ └── init.lua
38+
├── LICENSE
39+
└── README.MD
5540
5641
```
5742

58-
## Usage:
43+
## Documentation:
44+
45+
Not ready yet. I am lazy. **But if you create an issue** so I can see that someone needs it, I will create code documentation instantly ⚡.
46+
47+
But you can look at the examples below to understand what is what. Also, in the examples there are README files with a lot of useful information.
48+
49+
## Application examples:
5950

60-
- 🔥 [application](/lua/long-polling/application)
61-
- [client](/lua/long-polling/client)
62-
- [server](/lua/long-polling/server)
51+
- 🔥 [advanced](/examples/advanced). Source code of real-working application
52+
- [simple](/examples/simple). Simple to understand example of using the library
6353

6454
## TODO:
6555

examples/advanced/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM ghcr.io/trigonim/lua-express:latest
33
RUN luarocks install copas \
44
&& luarocks install lua-cjson \
55
&& luarocks install lua-express-middlewares \
6-
&& luarocks install lua-long-polling \
6+
# && luarocks install lua-long-polling \
77
&& luarocks install redis-lua
88

99

@@ -14,7 +14,8 @@ EXPOSE 3000
1414
COPY . /app
1515
WORKDIR /app
1616

17-
# you can set this by passing this to docker run command
17+
# you can set this by passing variables to docker run command
1818
# ENV TZ=
19+
# ENV LUA_PATH="/app/lua/?.lua;/app/lua/?/init.lua;${LUA_PATH}"
1920

2021
CMD ["lua", "server.lua"]

examples/advanced/README.MD

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
# Long Polling Application
1+
# Long Polling Server Application
22

3-
> 🇺🇸 English version not ready for now (I am lazy and don't want to translate it). You can use https://www.deepl.com/ to translate text on your own.
3+
In this folder you can find the source code of the server and client, which works at `poll.def.pm`.
4+
5+
## Features
6+
7+
- Can be used to deliver messages to multiple clients at the same time.
8+
- Tested in a production environment
9+
- docker ready
10+
- You can specify predefined parameters to be added to each update object, which is useful when you want to recognize different services that send updates to the same link.
11+
- Ability to customize the amount of data stored and the storage time (for redis) so that no garbage is stored
412

5-
В папке находится исходный код поллинг клиента и сервера, который работает по адресу `poll.def.pm`.
613

7-
## Run advanced application
14+
## Run the application
815

916
```bash
10-
cd directory_with_this_file/
17+
# git clone this repo
18+
# cd directory_with_this_file/
1119

1220
# run server via docker-compose.yml (will build fresh image)
21+
# 👍 nice for development
1322
docker-compose up --build polling
1423

1524
# alternatively run server via docker run (will use image from registry)
@@ -32,36 +41,36 @@ No authorization. With API features in mind, you can use your server as a public
3241

3342
**👀 Test server:** poll.def.pm. But please, don't use it in production because I can turn it off at any moment.
3443

35-
# Lua Long Polling Client Example
44+
## Client Example
3645

3746
> 🇺🇸 English version not ready for now (I am lazy and don't want to translate it). You can use https://www.deepl.com/ to translate text on your own.
3847
39-
Работает как в чистом Lua, так и на Garry's Mod сервере.
48+
Works both in pure Lua and on Garry's Mod server.
4049

41-
На чистом Lua требует copas (а он luasocket), lua-requests-async (он тоже требует copas) и cjson.
50+
On pure Lua requires copas (and luasocket), lua-requests-async (it also requires copas) and cjson.
4251

43-
## Использование
52+
### Usage
4453

45-
**В гмоде** закинуть по любому пути, потом получить доступ к библиотеке через `kupol = include("path/to/client.lua")`. Автоматически в глобальное пространство ничего не добавляется.
54+
**In gmod** put any file in the path, then get access to the library through `kupol = include("path/to/client.lua")`. Nothing is added to the global space automatically.
4655

47-
В чистом Lua можно использовать `kupol = require("long-polling.client")`.
56+
In pure Lua you can use `kupol = require("long-polling.client")`.
4857

49-
## Пример
58+
### Example
5059

5160
```lua
5261
local kupol = require("long-polling.client") -- also works in Garry's Mod with include()
5362
local Client = kupol.new("https://poll.def.pm/SomeSecretWord")
5463

5564

56-
-- Получение данных
65+
-- Getting data
5766
Client:subscribe(function(upd, last_id)
58-
print("LP сервер за все время принял " .. tostring(last_id) .. " обновлений")
59-
print("Последнее обновление: " .. kupol.json_encode(upd))
60-
end, nil, 30) -- 30 это timeout в секундах.
61-
-- nil – это последний id, который был получен (offset)
67+
print("LP server received " .. tostring(last_id) .. " updates in all time")
68+
print("Last update: " .. kupol.json_encode(upd))
69+
end, nil, 30) -- 30 is timeout in seconds.
70+
-- nil – this is the last id, which was received (offset)
6271

6372

64-
-- Отправка данных
73+
-- Sending data
6574
kupol.thread_new(function()
6675
for i = 1, 10 do
6776
local ok, err = Client:publish({any = "value", counter = i})

examples/advanced/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
- RATE_LIMIT_LIMIT=120
3535
volumes: # development
3636
# - $PWD/../../:/app
37-
- $PWD/../../lua/long-polling:/usr/local/share/lua/5.1/long-polling
37+
- $PWD/../../lua/long-polling:/usr/local/share/lua/5.1/long-polling:ro # luarocks install and lua search path
3838
init: true # to kill faster via ctrl + c
3939
# command: lua -e 'while true do end' # if you need to exec bash
4040
ports:

examples/simple/README.MD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Simple Long Polling Server
2+
3+
This is a simplest realization of long-polling server.
4+
5+
All data is stored in RAM, so it will be lost after server restart.
6+
7+
There is no request logging or error handling features implemented,
8+
but you can find it in advanced app file located somewhere in this repo 😅
9+
10+
## API:
11+
12+
- `POST` example.com/any_string?data=any_data => 200 OK
13+
- `GET ` example.com/any_string?last_id=0&timeout=60 => 200 OK {"any_data"}

examples/simple/server.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local polling = require("long-polling.server").new("localtable")
2+
3+
local express = require("express")
4+
local app = express()
5+
6+
local function push_updates(req, res)
7+
local channel = req.params.channel
8+
local payload = req.query.data
9+
10+
polling:publish_new(channel, payload)
11+
res:send("OK")
12+
end
13+
14+
local function get_updates(req, res)
15+
local channel = req.params.channel
16+
local last_id = tonumber(req.query.last_id) or 0
17+
local timeout = tonumber(req.query.timeout) or 0
18+
19+
local data, total = polling:get_news(channel, last_id, timeout)
20+
res:set("X-Total-Messages", total):json(data)
21+
end
22+
23+
app:post("/:channel", push_updates)
24+
app:get("/:channel", get_updates)
25+
26+
app:listen(3000)

0 commit comments

Comments
 (0)