FubuMVC now has support for Server Sent Events (SSE)! I have recently used it for our internal call center dashboard here at Extend Health. Since it’s inception we are now seeing data on the dashboard within seconds of it happening, as opposed to the previous 5 second ajax refresh interval we were doing before.
Out of the box (OOTB) the FubuMVC.ServerSentEvents bottle already does just about everything you need on the server. The only thing you need to implement is a topic for which your events will be coordinated against. A topic is created by inheriting the
FubuMVC.ServerSentEvents.Topic class. The topic will be used to coordinate how events are stored and pushed to the connected clients. This is accomplished by overriding the
bool Eqauls(object item) and
int GetHashCode() methods. See an example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
When an event is published or when a client connects, the code will pass through a TopicFamily which is keyed by the topic object you define. A separate channel is created for each topic, which is where the method overrides come in. The diagram below is a BASIC flow of the how events are stored and retrieved:
Now all you need to do is publish your events and connect a client. You publish events via the
IEventPublisher. I will cover the event and how you can customize it in a future post. For now though the
ServerEvent class is the basic OOTB event that you can use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Now the client can connect to the stream via
http://<YOUR HOST>/_events/<TOPIC NAME> which would be
http://localhost:8080/_events/SimpleTopic in my example.
The standard protocol for SSE according to W3C specifies that events should follow the format:
1 2 3 4 5
The bottle takes a slightly different stance on this subject. However, you can override it with your own if you like by replacing the
IServerEventWriter service with your own implementation. The stream that comes OOTB follows this format:
1 2 3 4
This is done so that you only need to setup one event listener on the client, from which you can build up a standard for processing events on the client. Which I plan to describe more in future posts.