forwardUnderstanding Session States   Table of ContentUpdating GUI Controls on Session Eventforward
Subscribing to Session Events

The following table describes the meaning of all events exposed by the Session class.

EventDescription
StateChangeEvent Session state is changed.
ErrorEvent Error condition is detected.
InboundApplicationMsgEvent Application-level message is received from the counterparty.
InboundSessionMsgEvent Session-level message is received from the counterparty.
OutboundApplicationMsgEvent Application-level message will be sent to the counterparty.
OutboundSessionMsgEvent Session-level message will be sent to the counterparty.
MessageResending Sent application-level message is about to be resent to the counterparty.
ResendingStarted ResendRequest message is received and the session is about to start resending messages.
ResendingFinished Resending process is completed.
MessageSending Sending message to the counterparty.
WarningEvent Warning condition is detected.
BytesReceived Bytes are received by a session.

Note Note
Session event handling is a synchronous operation. For this reason, it is recommended not to perform time-consuming tasks inside event handlers.
Note Note
Session methods that change Session's state (e.g. Logout()) should not be called from event handlers directly. A new thread should be launched to execute such a method.
Example

The following example subscribes to all session events. It becomes useful to get a general understanding of the session behavior.

C#
void OnStateChange(object sender, StateChangeEventArgs e)
{
    Console.WriteLine("New session state: {0}", e.NewState);
}

void OnInboundApplicationMsg(object sender, InboundApplicationMsgEventArgs e)
{
    Console.WriteLine("Incoming application-level message: {0}", e.Msg);
}

void OnInboundSessionMsg(object sender, InboundSessionMsgEventArgs e)
{
    Console.WriteLine("Incoming session-level message: {0}", e.Msg);
}

void OnOutboundApplicationMsg(object sender, OutboundApplicationMsgEventArgs e)
{
    Console.WriteLine("Outgoing application-level message: {0}", e.Msg);
}

void OnOutboundSessionMsg(object sender, OutboundSessionMsgEventArgs e)
{
    Console.WriteLine("Outbound session-level message: {0}", e.Msg);
}

void OnMessageResending(object sender, MessageResendingEventArgs e)
{
    Console.WriteLine("OnMessageResending: {0}", e.Msg);
    e.AllowResending = false;
}

void OnResendingStarted(object sender, ResendingStartedFinishedEventArgs e)
{
    Console.WriteLine("OnResendingStarted: BeginSeqNo={0}, EndSeqNo={1}", e.BeginSeqNo, e.EndSeqNo);
}

void OnResendingFinished(object sender, ResendingStartedFinishedEventArgs e)
{
    Console.WriteLine("OnResendingFinished: BeginSeqNo={0}, EndSeqNo={1}", e.BeginSeqNo, e.EndSeqNo);
}

void OnMessageSending(int msgSeqNum)
{
    Console.WriteLine("OnMessageSending"");
}

void OnSessionWarning(object sender, WarningEventArgs e)
{
    Console.WriteLine("Session warning: {0}", e.Reason);
}

void OnSessionError(object sender, ErrorEventArgs e)
{
    Console.WriteLine("Session error: {0}", e.Reason);
}

void run()
{
    Session session = new Session("SenderCompID", "TargetCompID", ProtocolVersion.FIX44);

    session.StateChangeEvent += new Session.StateChangeEventHandler(OnStateChange);
    session.InboundSessionMsgEvent += new Session.InboundSessionMsgEventHandler(OnInboundSessionMsg);
    session.InboundApplicationMsgEvent += new Session.InboundApplicationMsgEventHandler(OnInboundApplicationMsg);
    session.OutboundSessionMsgEvent += new Session.OutboundSessionMsgEventHandler(OnOutboundSessionMsg);
    session.OutboundApplicationMsgEvent += new Session.OutboundApplicationMsgEventHandler(OnOutboundApplicationMsg);
    session.MessageResending += new Session.MessageResendingEventHandler(OnMessageResending);
    session.ResendingStarted += new Session.ResendingStartedEventHandler(OnResendingStarted);
    session.ResendingFinished += new Session.ResendingFinishedEventHandler(OnResendingFinished);
    session.MessageSending += new Session.MessageSendingEventHandler(OnMessageSending);
    session.WarningEvent += new Session.WarningEventHandler(OnSessionWarning);
    session.ErrorEvent += new Session.ErrorEventHandler(OnSessionError);

    // ..
}
VB
Sub OnStateChange(ByVal sender As Object, ByVal e As StateChangeEventArgs)
    Console.WriteLine("New session state: {0}", e.NewState)
End Sub

Sub OnInboundApplicationMsg(ByVal sender As Object, ByVal e As InboundApplicationMsgEventArgs)
    Console.WriteLine("Incoming application-level message: {0}", e.Msg)
End Sub

Sub OnInboundSessionMsg(ByVal sender As Object, ByVal e As InboundSessionMsgEventArgs)
    Console.WriteLine("Incoming session-level message: {0}", e.Msg)
End Sub

Sub OnOutboundApplicationMsg(ByVal sender As Object, ByVal e As OutboundApplicationMsgEventArgs)
    Console.WriteLine("Outgoing application-level message: {0}", e.Msg)
End Sub

Sub OnOutboundSessionMsg(ByVal sender As Object, ByVal e As OutboundSessionMsgEventArgs)
    Console.WriteLine("Outbound session-level message: {0}", e.Msg)
End Sub

Sub OnMessageResending(ByVal sender As Object, ByVal e As MessageResendingEventArgs)
    Console.WriteLine("OnMessageResending: {0}", e.Msg)
    e.AllowResending = False
End Sub

Sub OnResendingStarted(ByVal sender As Object, ByVal e As ResendingStartedFinishedEventArgs)
    Console.WriteLine("OnResendingStarted: BeginSeqNo={0}, EndSeqNo={1}", e.BeginSeqNo, e.EndSeqNo);
End Sub

Sub OnResendingFinished(ByVal sender As Object, ByVal e As ResendingStartedFinishedEventArgs)
    Console.WriteLine("OnResendingFinished: BeginSeqNo={0}, EndSeqNo={1}", e.BeginSeqNo, e.EndSeqNo);
End Sub

Sub OnMessageSending(ByVal msgSeqNum As Integer)
    Console.WriteLine("OnMessageSending")
End Sub

Sub OnSessionWarning(ByVal sender As Object, ByVal e As WarningEventArgs)
    Console.WriteLine("Session warning: {0}", e.Reason)
End Sub

Sub OnSessionError(ByVal sender As [Object], ByVal e As ErrorEventArgs)
    Console.WriteLine("Session error: {0}", e.Reason)
End Sub

Sub MainRun()
    Dim session As New Session("SenderCompID", "TargetCompID", FIXForge.NET.FIX.ProtocolVersion.FIX44)

    AddHandler session.StateChangeEvent, AddressOf OnStateChange
    AddHandler session.InboundSessionMsgEvent, AddressOf OnInboundSessionMsg
    AddHandler session.InboundApplicationMsgEvent, AddressOf OnInboundApplicationMsg
    AddHandler session.OutboundSessionMsgEvent, AddressOf OnOutboundSessionMsg
    AddHandler session.OutboundApplicationMsgEvent, AddressOf OnOutboundApplicationMsg
    AddHandler session.MessageResending, AddressOf OnMessageResending
    AddHandler session.ResendingStarted, AddressOf OnResendingStarted
    AddHandler session.ResendingFinished, AddressOf OnResendingFinished
    AddHandler session.MessageSending, AddressOf OnMessageSending
    AddHandler session.WarningEvent, AddressOf OnSessionWarning
    AddHandler session.ErrorEvent, AddressOf OnSessionError

    ' ..
End Sub
Note Note
To modify outgoing messages you need to use the following approach:
C#
void OnOutboundSessionMsg(object sender, OutboundSessionMsgEventArgs e)
{
    e.ModifiedMsg = e.Msg;
    //You should use the ModifiedMsg property to modify the outgoing message
    e.ModifiedMsg.Remove(Tags.ApplVerID);
    Console.WriteLine("Outbound modified session-level message: {0}", e.ModifiedMsg);
}
Content