Json messages are not decoded transparently
When a struct is sent over a channel it is turned to json. Then when it is received on the other end it is meant to be transparently turned back to that struct. Instead, a json string is received.
However this works correctly when using the history API. This means the messages are being correctly sent but not received.
The history code also converts to a generic map instead of the struct. Both should be changed to return the struct.
Clients may rely on this bad behaviour so this change will need to be for v2.
The history code also converts to a generic map instead of the struct. Both should be changed to return the struct.
I think this will need some form of struct registration, because the SDK won't be able to infer which in-memory struct to decode into without being told about it.
It may be worth considering a Message.Decode(dst interface{}) method which decodes msg.Data into the provided object:
channel.Subscribe(ctx, func(msg *ably.Message) {
var data MyStruct
if err := msg.Decode(&data); err != nil {
return
}
// do something with data
})
Longer term we'd like to have first class support for schemas (e.g. register a protobuf definition for a channel, and the SDK decodes messages based on that definition).
It may be worth considering a
Message.Decode(dst interface{})method
This is a wider question than just for this library and should be discussed as such. @QuintinWillison