public class NewBookMessage
public NewBookMessage(string name)
public string BookName { get; }
public class BookPublisher: ReceiveActor
Receive<NewBookMessage>(x => Handle(x));
private void Handle(NewBookMessage x)
Context.System.EventStream.Publish(x);
public class BookSubscriber: ReceiveActor
Receive<NewBookMessage>(x => HandleNewBookMessage(x));
private void HandleNewBookMessage(NewBookMessage book)
Console.WriteLine($"Book: {book.BookName} got published - message received by {Self.Path.Name}!");
public static void Main()
ActorSystem system = ActorSystem.Create("pub-sub-example");
var publisher = system.ActorOf<BookPublisher>("book-publisher");
var subscriber1 = system.ActorOf<BookSubscriber>("book-subscriber1");
var subscriber2 = system.ActorOf<BookSubscriber>("book-subscriber2");
system.EventStream.Subscribe(subscriber1, typeof(NewBookMessage));
system.EventStream.Subscribe(subscriber2, typeof(NewBookMessage));
publisher.Tell(new NewBookMessage("Don Quixote"));
publisher.Tell(new NewBookMessage("War and Peace"));