Question: How can I proxy the raw message without serializing it? #158
-
Firstly, thank you for creating this excellent package! One question I have though is; I would like to write the raw string message data from a MQTT inbound channel directly on to a Kafka outbound channel. No serialization required - publish it out exactly as it comes in. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
There are multiple ways to achieve your goal. 1. Using the
|
Beta Was this translation helpful? Give feedback.
-
Since you are processing a message and you are therefore in a subscriber, you can also take advantage of the auto-publishing feature, when you chose either solution 1 or 2. public class MySubscriber
{
public MyBinaryMessage ProcessMqttMessage(YourMqttMessage message) => new MyBinaryMessage(Encoding.UTF8.GetBytes(...));
} See also https://silverback-messaging.net/concepts/bus/subscribe.html#return-new-messages-republishing. |
Beta Was this translation helpful? Give feedback.
-
Thanks @BEagle1984 For reference for anyone wondering how to do the same things Modelusing System.IO;
using Silverback.Messaging.Messages;
public class PlainMessage : BinaryFileMessage, IEvent
{
public override string ToString()
{
return Content is {CanRead: true}
? new StreamReader(Content).ReadToEnd()
: string.Empty;
}
} Inbound Endpoints Configuratorusing MQTTnet.Formatter;
using MQTTnet.Protocol;
using Silverback.Messaging.Configuration;
public class MqttConfigurator : IEndpointsConfigurator
{
public void Configure(IEndpointsConfigurationBuilder builder)
{
builder.AddMqttEndpoints(endpoints =>
endpoints
.Configure(config => config
.ConnectViaTcp("localhost", 8883)
.EnableTls()
)
.AddInbound(eb => eb
.ConsumeFrom("message/#")
.Configure(c => { c.ClientId = $"{_options.Value.ClientId}.{nameof(PlainMessage)}"; })
.ConsumeBinaryFiles()
)
);
}
} Subscriberpublic class BinaryFileMessageSubscriber
{
private readonly ILogger<BinaryFileMessageSubscriber> _logger;
public BinaryFileMessageSubscriber(ILogger<BinaryFileMessageSubscriber> logger)
{
_logger = logger;
}
public PlainMessage? OnMessageReceived(IBinaryFileMessage message)
{
var m = new PlainMessage(message.Content, "application/json");
_logger.LogInformation("BinaryMessage: {Message}", m.ToString());
return m;
}
} Outbound Endpoints Configuratorpublic class KafkaConfigurator : IEndpointsConfigurator
{
public void Configure(IEndpointsConfigurationBuilder builder)
{
builder
.AddKafkaEndpoints(endpoints => endpoints
.Configure(kcc => { })
.AddOutbound<PlainMessage>(obe =>
obe.ProduceTo("raw-messages")
)
);
}
} |
Beta Was this translation helpful? Give feedback.
There are multiple ways to achieve your goal.
1. Using the
BinaryFileMessage
As the name suggests, this was meant to transfer binary files, but you can leverage it to publish any binary content including your UTF8 encoded string.