How to keep listening in FP?

Viewed 134

I want to use Kafka for sending messages between microservices.

Kafka uses publish subscribe mechanism, that it is mean, a producer produces messages, kafka receives it and subscribes to consumers. Consumers always listen to an events for incoming messages and react accordingly.

My question is, how to build a system with functional programming language(scala, haskell), for listening incoming events?

It is like a http server, that is listening for incoming requests.

I think, it needs an IO Monad because of the side effects, that is going the happen. And how to keep it always running? With a simple look?

1 Answers

I don't know Scala, but in Haskell the answer is, exactly like you would in an imperative language, but in the IO monad.

There may be systems like Functional Reactive Programming that let you write listeners in a functional way, but ultimately they're declarative languages for generating an imperative program, and under the hood they're going to be imperative.

Some people consider Haskell to be their favorite imperative language! The idea is not that Haskell has no side effects, but that the type system makes it explicitly clear where they occur and in what order.

Related