Flat Message
Sometimes it is more convenient to work with FIX messages just as sequences of tag=value
pairs.
For example, an application needs to receive a message, access a few fields,
and use the value of these fields to route the message to one of the counterparties (so-called "FIX to FIX routing").
In this scenario, it could be impractical to maintain the FIX Dictionary - especially
if messages are received from venues that use different FIX Dictionaries.
To support this scenario, the Engine exposes the FlatMessage class.
Constructing Flat Message
To create a new FlatMessage
object, use one of the FlatMessage constructors.
For example:
var message = new FlatMessage(ProtocolVersion.Fix44);
Adding Field
To add a field, use the Add(int, string) method. This method adds the given tag/value pair at the end of the message instance.
For example:
message.Add(OnixS.Fix.Fix44.Tag.ClOrdID, "ClOrdID value");
Accessing Field Value
The flat message field value can be accessed using either the field index or the tag number:
Note
When the field is accessed by the tag number, the linear search is performed until the tag is found.
The Get(int, int) method has the hintFieldIndex
argument to set the search's starting point.
For example:
var clientOrderIdByTag = message.Get(OnixS.Fix.Fix44.Tag.ClOrdID);
var clientOrderIdByTagAndHint = message.Get(OnixS.Fix.Fix44.Tag.ClOrdID, 3);
var clientOrderIdByIndex = message.GetByIndex(5);
Accessing Repeating Fields
See Flat Group Reader.
Removing Field
To remove a field, use the Remove(int) and RemoveLastField() methods.
Enumerating Fields
The FlatMessage class supports the IEnumerable
interface,
so you can use a FlatMessage instance in the foreach
operator to enumerate message fields.
For example:
foreach (Field field in message)
{
System.Console.WriteLine($"{field.Tag}={field.Value}");
}
Receiving Flat Messages
To receive Flat Messages, set Session's MessageMode
property to FlatMessage.
In this mode, MessageEventArgs.FlatMessage
will contain the reference
to the received FlatMessage instance.
For example:
using var initiator = new Session(TargetCompID, SenderCompID, dictionary)
{
MessageMode = MessageMode.FlatMessage
};
Sending Flat Messages
To send Flat Messages, use the Session.Send(FlatMessage) method method.
FlatMessage
versus Message
Class | Advantages | Disadvantages |
---|---|---|
Message |
Supports validation. | Fields order is defined by the FIX Dictionary. |
Tag-based field access takes constant time. | Repeating group fields are lost without the complete FIX Dictionary. | |
The message type and FIX protocol version cannot be changed. | ||
FlatMessage |
Repeating group fields are always preserved and can be accessed without the corresponding FIX Dictionary. | Tag-based field access takes linear time. |
Keeps the field order. | Does not support validation. | |
Any field can be changed. |
See Also
- The FlatMessage sample from the FIX Engine distribution package
- Flat Group Reader