Created
January 13, 2012 07:39
-
-
Save seburgi/1605014 to your computer and use it in GitHub Desktop.
Lokad.CQRS.Sample + ZeroMQ = client update notifications (https://groups.google.com/d/topic/lokad/kD53JYzkV0o/discussion)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see Google Groups topic at https://groups.google.com/d/topic/lokad/kD53JYzkV0o/discussion | |
// example event handler in a projection | |
// _clients is an instance of IClientNotificationService | |
public void When(Source<NewAppointmentRegistered> source) | |
{ | |
NewAppointmentRegistered e = source.Event; | |
DateTime dateTime = e.DateTime.Date; | |
_writer.UpdateEnforcingNew(dateTime, dayDto => AddNewAppointmentToDayDto(dayDto, e.Id, ...)); | |
_clients.Publish("day-" + dateTime.ToString(DateTimeFormat)); | |
} | |
// ZeroMQ publisher (server) | |
class ClientNotificationService : IClientNotificationService, IDisposable | |
{ | |
private readonly Context _context; | |
private readonly Socket _publisher; | |
public ClientNotificationService() | |
{ | |
_context = new Context(1); | |
_publisher = _context.Socket(SocketType.PUB); | |
_publisher.Bind("tcp://*:5556"); | |
} | |
public void Publish(string message) | |
{ | |
_publisher.Send(message, Encoding.Unicode); | |
} | |
public void Dispose() | |
{ | |
_publisher.Dispose(); | |
_context.Dispose(); | |
} | |
} | |
// ZeroMQ subscriber (client) | |
var ctx = new Context(1); | |
Task.Factory.StartNew(() => | |
{ | |
using (Socket eh = ctx.Socket(SocketType.SUB)) | |
{ | |
eh.Connect("tcp://localhost:5556"); | |
eh.Subscribe("", Encoding.Unicode); | |
while (true) | |
{ | |
string message = eh.Recv(Encoding.Unicode); | |
if (message.StartsWith("day")) ReloadDay(message.Split('-')[1]); | |
else if (message == "...") ...; | |
... | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice concept!
Posted a few suggestions back to the discussion: https://groups.google.com/d/topic/lokad/kD53JYzkV0o/discussion