9
votes

I'm looking for how to connect to a redis server via Unix domain socket using hedis, as advertised in the hackage page:

Connect via TCP or Unix Domain Socket:
TCP sockets are the default way to connect to a Redis server. For connections to a server on the same machine, Unix domain sockets offer higher performance than the standard TCP connection.

From the constructors of ConnectInfo, as well as the defaultConnectInfo, it seems that we should fill in the connectPort, since it has type PortID which has a constructor named UnixSocket. But it only shows UnixSocket is a String, without details of format, etc.

So how to fill in the connectPort to connect via Unix domain socket? Thanks.


UPDATE: I tried it out and found it's not that hard. Below is my hello world.

{-# LANGUAGE OverloadedStrings #-}
import Control.Monad.Trans
import Database.Redis

myConnectInfo :: ConnectInfo
myConnectInfo = defaultConnectInfo { connectPort = UnixSocket "/tmp/redis.sock" }

main :: IO ()
main = do
    conn <- connect myConnectInfo
    runRedis conn $ do
        set "hello" "hello"
        set "world" "world"
        hello <- get "hello"
        world <- get "world"
        liftIO $ print (hello,world)
1

1 Answers

7
votes

I'm not a Haskell user at all, and I cannot test it, but I would say you just have to provide the path of the socket file in this string.

Instead of:

connectPort           = PortNumber 6379

you would have:

connectPort           = UnixSocket "/tmp/redis.sock"

Of course, /tmp/redis.sock should be declared in the server-side Redis configuration file using the following parameters:

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /tmp/redis.sock
unixsocketperm 755

Please note by default, unix domain socket parameters are commented out.