Matt Smith's Blog

Software Engineering related topics

Event Serialization in FubuMVC.ServerSentEvents

| Comments

The IServerEvent.Data property is defined by the interface to be an object. Hence this object needs to be serialized prior to broadcasting it down to connected clients. By default the FubuMVC.ServerSentEvents bottle will use the FubuMVC JSON serializer. So an example stream would look like this:

Example of stream with serialized data
1
2
3
4
id: <EVENT ID>/<EVENT NAME>\n
data: { "property" : "value" }\n
retry: <RECONNECTION TIME>\n
\n

You can easily change the serializer by implementing your own version of IDataFormatter then in your registry add ReplaceService<IDataFormatter, YOURIMPLEMENTATION>()

This is where appending the event name to the end of the event id comes in handy. You can route all of your events to the default message handler on the client, deserialize the data, then trigger a non-SSE event with the deserialized data, similar to how everything else is handled in FubuMVC.

Comments