Skip to main content
1 of 4
egerhard
  • 220
  • 1
  • 8

Simple RabbitMQ client wrapper

I'm writing a small F# module to wrap the .Net RabbitMQ client. The goal is to make interacting with RabbitMQ a little more functional.

I'm looking to get some feedback on the quality of my F# code as well as what patterns or best practices I should be using to make this better.

module Client =
    let connectToRabbitMqServerAt address = 
        let factory = new ConnectionFactory(HostName = address)
        factory.CreateConnection()

    let openChannelOn (connection:IConnection) = connection.CreateModel()

    let private declareQueue (channel:IModel) queueName = 
        channel.QueueDeclare( queueName, false, false, false, null )

    let private publishToQueue (channel:IModel) queueName (message:string) =
        let body = Encoding.UTF8.GetBytes(message)
        channel.BasicPublish("", queueName, null, body)
        
    let createQueueReader channel queue = 
        declareQueue channel queue |> ignore
        
        fun () -> 
            let ea = channel.BasicGet(queue, true)
            if ea <> null then
                let body = ea.Body
                let message = Encoding.UTF8.GetString(body)
                Some message
            else
                None

    let createQueueWriter channel queue =
        declareQueue channel queue |> ignore
        publishToQueue channel queue
egerhard
  • 220
  • 1
  • 8