ably-dotnet
ably-dotnet copied to clipboard
Allow to send raw jtoken/jobject instead object
Allow to send raw jtoken/jobject instead object in IRestChannel.PublishAsync
Hi @mcdis. Can you elaborate on your use case please?
Hi @mattheworiordan! We have got micro services arch and one type of service is targeting to collecting data in json and sending them to configured channel to configured queue system. One of available target queue system is Ably. So message aggregation works with not typed json messages and at this moment we are converting them to string because raw jobject publishing doesn't work well.
Thanks for the response, will get @withakay to look at soon.
Hi @mcdis. There might be a small issue depending on which version of the library you are using. We currently bundle json.net with the ably binary in .net and xamarin to stop people having issues if they are using older version of the json.net. I don't know if exposing a jtoken class will be the same as you are using.
Currently there is a way to pass json directly. You can package it an encoded Message object as described in this ticket: https://github.com/ably/ably-dotnet/issues/92#issuecomment-370090243
You can pass a json object to ably at the moment and you will get a JObject back on the other side. I'm sure this will work fine if the services are .net core but need to test it in .Net Framework.
If you want to send a json string and you don't want ably to try and decode it on the way back then you can create a message with a custom encoding and the library won't touch the json string so you can choose whether to parse it or not. Here is a test showing how that works:
public async Task SendAndReceiveJson(Protocol protocol)
{
var client = await GetRealtimeClient(protocol);
await client.WaitForState(ConnectionState.Connected);
var jobject = JObject.Parse("{ \"test\":\"test\"}");
void PublishJson(IRealtimeChannel c, string name, JToken jsonToken)
{
var ablyMessage = new Message(name, jsonToken.ToString()) { Encoding = "custom" };
c.Publish(ablyMessage);
}
var channel = client.Channels.Get("test");
var awaiter = new TaskCompletionAwaiter();
channel.Subscribe(message =>
{
message.Encoding.Should().Be("custom");
message.Data.Should().BeOfType<string>();
awaiter.Tick();
});
PublishJson(channel, "json", jobject);
await awaiter.Task;
}