2018-12-16 21:39:43 +01:00
# Redis Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2015-02-06 | [Vladimir Dronnikov ](https://github.com/dvv ) | [Vladimir Dronnikov ](https://github.com/dvv ) | [redis.lua ](../../lua_modules/redis/redis.lua ) |
2018-12-16 21:39:43 +01:00
This Lua module provides a simple implementation of a [Redis ](https://redis.io/ ) client.
### Require
```lua
redis = dofile("redis.lua")
```
### Release
```lua
redis = nil
```
## redis.connect()
Function used to connect to Redis server.
#### Syntax
`redis.connect(host, [port])`
#### Parameters
2019-04-19 16:04:10 +02:00
- `host` Redis host name or address
- `port` Redis database port. Default value is 6379.
2018-12-16 21:39:43 +01:00
#### Returns
Object with rest of the functions.
2019-04-19 16:04:10 +02:00
!!! important
You need to start calling this `connect()` function to obtain a Redis object. All other functions are invoked on this object. Note the difference between `redis.connect()` (single dot) and `redis:subscribe()` (colon).
## redis:subscribe()
2018-12-16 21:39:43 +01:00
Subscribe to a Redis channel.
#### Syntax
`redis:subscribe(channel, handler)`
#### Parameters
2019-04-19 16:04:10 +02:00
- `channel` Channel name
- `handler` Handler function that will be called on new message in subscribed channel
2018-12-16 21:39:43 +01:00
#### Returns
`nil`
## redis:publish()
Publish a message to a Redis channel.
#### Syntax
`redis:publish(channel, message)`
#### Parameters
2019-04-19 16:04:10 +02:00
- `channel` Channel name
- `message` Message to publish
2018-12-16 21:39:43 +01:00
#### Returns
`nil`
## redis:unsubscribe()
Unsubscribes from a channel.
#### Syntax
`redis:unsubscribe(channel)`
#### Parameters
2019-04-19 16:04:10 +02:00
- `channel` Channel name to unsubscribe from
2018-12-16 21:39:43 +01:00
#### Returns
`nil`
2019-04-19 16:04:10 +02:00
## redis:close()
2018-12-16 21:39:43 +01:00
Function to close connection to Redis server.
#### Syntax
`redis:close()`
#### Parameters
None
#### Returns
`nil`
2019-04-19 16:04:10 +02:00
## Example
2018-12-16 21:39:43 +01:00
```lua
local redis = dofile("redis.lua").connect(host, port)
2019-04-19 16:04:10 +02:00
redis:publish("chan1", "foo")
redis:subscribe("chan1", function(channel, msg)
print(channel, msg)
end)
2019-01-13 22:01:57 +01:00
```