You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Michel Martens 127d8790b8 Merge pull request #3 from ignacio/patch-1 6 years ago
LICENSE Initial commit 7 years ago Document resp.encode 7 years ago
resp.lua Fallback to old 'unpack' if available. 6 years ago
resp_test.lua Document resp.encode 7 years ago


Lightweight RESP client


Lightweight RESP client that can be used for interacting with Redis servers.


local resp = require("resp")
local client ="localhost", 6379)

assert("OK" == client:call("SET", "foo", "42"))
assert("42" == client:call("GET", "foo"))


You can pipeline commands by using the queue/commit methods.

local resp = require("resp")
local client ="localhost", 6379)

client:queue("ECHO", "foo")
client:queue("ECHO", "bar")

assert_equal(#client.buff, 2)

result = client:commit()

assert_equal(#client.buff, 0)

assert_equal(result[1], "foo")
assert_equal(result[2], "bar")


For commands like MONITOR and SUBSCRIBE, you can keep reading messages from the server:

local resp = require("resp")
local c1 ="localhost", 6379)
local c2 ="localhost", 6379)

-- Subscribe to channel "foo"
c1:call("SUBSCRIBE", "foo")

-- Publish to channel "foo"
c2:call("PUBLISH", "foo", "hello")
c2:call("PUBLISH", "foo", "world")

r1 = c1:read()
r2 = c1:read()

-- Messages have type, channel and content
assert_equal(r1[1], "message")
assert_equal(r1[2], "foo")
assert_equal(r1[3], "hello")

assert_equal(r2[1], "message")
assert_equal(r2[2], "foo")
assert_equal(r2[3], "world")


Aside from creating a client, resp.lua can also be used to encode any message with the RESP protocol:

local resp = require("resp")

assert_equal("*1\r\n$3\r\nFOO\r\n\r\n", resp.encode("FOO"))


You need to have lsocket installed, then just copy resp.lua anywhere in your package.path.