# Sequencing

Sequencing is the methodology built within streams to allow message identifiers to be sequentially generated regardless of the shape of the channel. Messages are identified by an indexation position within the Tangle, and they are generated using a combination of these pieces of information:

• Application instance (channel identifier).
• Public key of the publisher.
• Previous message id (The message being linked to).
• Branch number (identifier for the specific branch).
• Sequencing number (the sequencing position of the publisher).

As messages are posted to and read from the channel, a local state for the user implementation will update with the message identifier, branch and sequencing numbers for each publishing party. This allows user implementations to derive and search for the next message in the sequence to keep in sync.

## Single Branch Sequencing​

In a single branch implementation, sequencing state of each user will be updated to the same state. This means that regardless of the publisher, the state of each user will update the previous message id to the new message link, and the sequencing number will be incremented by one.

Sequence states before Msg1 is sent:

               Author        Sub1Prev Msg Id     Msg0         Msg0 Branch No.        0            0Seq. No.          2            2       <- Users start from 2, 0 and 1 are reserved for subscriptions and announcements

Msg1 is then sent by the author using the above stated author state.

Sequence states after Msg1 is sent:

               Author        Sub1Prev Msg Id     Msg1         Msg1 Branch No.        0            0Seq. No.          3            3     
note

It is recommended that a single branch be used with only a single publisher to avoid out of sync parties from issuing and retrieving messages from an incorrect sequencing combination (i.e. Sub1 sends a message linked to Msg1 and Sub2 sends a message linked to that same Msg1 before seeing and registering the message from Sub1, thus forking the stream). Multiple publishers can be used, but it is important to ensure that they do not try to send messages in parallel to avoid this conflict. In the future this may be an enforced limitation on single branch implementations to remove the possibility altogether.

## Multi Branch Sequencing​

In a multi branch implementation, the sequencing state of each user will be updated independently after each message is sent. In order to track the linking of messages of individual publishers within a tree-like structure, a secondary message is sent in tandem with every data message. This message is called a sequencing message, and contains the essence necessary for a user to derive the correct message id of a sequenced message. The sequencing messages are issued to an anchored branch generated during the creation of the channel. As a new message is generated by a user, a sequencing message is issued to this anchored branch to allow users a reference guide to the location of the issued data packet.