Publisher subscribe to Scala

I am new to scala and I am learning to use Publisher-Subscribe. Perhaps the problem is with Google skills, but I can’t find an example of this use, where I can specify which events should be followed by the subscriber.

Anyone have any examples?

Thank!

+5
source share
3 answers

If you need something really simple, then I propose to do it yourself, it should not be so difficult. But if you want something more, you can watch Eventbus . This is a Java lib, but I use it also with Scala.

Scala http://jim-mcbeath.blogspot.com/2009/10/simple-publishsubscribe-example-in.html

+4

http://comments.gmane.org/gmane.comp.lang.scala.user/63002:

case class MyEvent(number: Int)

class PrintIt extends Subscriber[MyEvent, Publisher[MyEvent]] {
  override def notify(pub: Publisher[MyEvent], event: MyEvent): Unit = {
    println("got an event: " + event)
  }
}

class RunIt extends Publisher[MyEvent] {
  def pub() = publish(MyEvent(12))
}

object Main extends App {
  val runIt = new RunIt()
  runIt.subscribe(new PrintIt)
  runIt.pub()
}
+4

Here you can find a simple implementation of EventBus. It also handles asynchronous events and publishes scheduled events.

https://github.com/hipjim/scala-event-bus

case class Msg(content: String)
val eb = EventBus()
eb.subscribe[Msg] { t =>
  println(t.content)
}

for (i <- 1 to 100)
 eb.post(Msg(i.toString))
+2
source

All Articles