23.1 SUBSCRIBE and NOTIFY: Event, Subscription-State, Expires
A call is not the only thing SIP can tell you about. A phone can ask to be told when something changes: whether a colleague is on the phone, whether new voicemail has arrived, whether its own registration is still valid. RFC 6665 defines the mechanism, with two methods:
“The general concept is that entities in the network can subscribe to resource or call state for various resources or calls in the network, and those entities (or entities acting on their behalf) can send notifications when those states change.”Read the section ↗
- The subscribersubscriber. The user agent that sends SUBSCRIBE and receives NOTIFY. sends SUBSCRIBE, with an Event header that names the event packageevent package. A named use of SUBSCRIBE and NOTIFY, such as presence or dialog, given in the Event header. It defines the body format and when to notify., and Expires, the time it wants the subscriptionsubscription. A request, made with SUBSCRIBE (or implied by REFER), to get NOTIFY requests when the state of a resource changes. It lasts for the time in Expires, unless the subscriber refreshes it. to last.
- The notifiernotifier. The user agent that accepts a SUBSCRIBE and sends NOTIFY requests with the state of the resource. answers 200 OK, with the Expires it grants, and at once sends a NOTIFY with the full current state. More NOTIFYs follow each time the state changes.
- Each NOTIFY carries Subscription-State:
active,pending, orterminated, with the time left inexpires, or the reason it ended. - SUBSCRIBE creates a dialog, like INVITE. Refreshes, the unsubscribe, and every NOTIFY travel inside it.
Flow builder · a subscription from start to end
Message waiting: Alice unsubscribes
- SIP
Alice's phone subscribes to her own mailbox. Event names the package; Expires asks for an hour.
All steps as text
- Alice → Voicemail server: SUBSCRIBE. Alice's phone subscribes to her own mailbox. Event names the package; Expires asks for an hour.
- Voicemail server → Alice: 200 OK. The notifier accepts the subscription. The 200 OK must carry Expires: the duration that counts.
- Voicemail server → Alice: NOTIFY (active). The first NOTIFY follows at once, with the full current state.
- Alice → Voicemail server: 200 OK (NOTIFY). Alice's phone confirms the NOTIFY.
- Voicemail server → Alice: NOTIFY (active). Bob leaves Alice a voicemail. Messages-Waiting: yes turns on her message lamp.
- Alice → Voicemail server: 200 OK (NOTIFY). Alice's phone confirms, and shows the new state.
- Alice → Voicemail server: SUBSCRIBE (refresh). Before the hour ends, Alice's phone refreshes the subscription in the same dialog, with the next CSeq.
- Voicemail server → Alice: 200 OK. The notifier extends the subscription by another hour.
- Voicemail server → Alice: NOTIFY (active). Each refresh also brings a NOTIFY with the full state.
- Alice → Voicemail server: 200 OK (NOTIFY). Alice's phone confirms the NOTIFY.
- Alice → Voicemail server: SUBSCRIBE (Expires: 0). Alice's phone unsubscribes: a SUBSCRIBE in the same dialog with Expires: 0.
- Voicemail server → Alice: 200 OK. The notifier accepts. The subscription now ends.
- Voicemail server → Alice: NOTIFY (terminated). A final NOTIFY says terminated, with the state one last time.
- Alice → Voicemail server: 200 OK (NOTIFY). Alice's phone confirms. The subscription is over.
“200-class responses to SUBSCRIBE requests also MUST contain an "Expires" header field. The period of time in the response MAY be shorter but MUST NOT be longer than specified in the request.”Read the section ↗
“Upon successfully accepting or refreshing a subscription, notifiers MUST send a NOTIFY request immediately to communicate the current resource state to the subscriber.”Read the section ↗
A subscription is soft state, like a registration: it disappears unless the subscriber refreshes it with another SUBSCRIBE in the same dialog. To end it early, the subscriber sends SUBSCRIBE with Expires: 0, and the notifier answers with a last NOTIFY that says terminated. Try End in the builder:
| Subscription-State | Meaning | What the subscriber does |
|---|---|---|
active;expires=3600 |
Accepted; 3600 s left | Refresh before the time runs out |
pending |
Received, not yet authorised | Wait for the next NOTIFY |
terminated;reason=timeout |
Not refreshed in time | Subscribe again, with a new dialog |
terminated;reason=deactivated |
The notifier is moving or restarting | Subscribe again at once |
terminated;reason=rejected |
No longer authorised | Do not subscribe again |
terminated;reason=noresource |
The resource no longer exists | Do not subscribe again |
“timeout: The subscription has been terminated because it was not refreshed before it expired. Clients MAY re-subscribe immediately.”Read the section ↗
One rule surprises many implementers. The first NOTIFY can arrive before the 200 OK to the SUBSCRIBE: the 200 OK can be lost or delayed on the way, and the NOTIFY overtakes it. Turn on NOTIFY before the 200 OK:
“Due to the potential for out-of-order messages, packet loss, and forking, the subscriber MUST be prepared to receive NOTIFY requests before the SUBSCRIBE transaction has completed.”Read the section ↗
23.2 Event packages: presence, dialog (BLF), message-summary (MWI), reg, refer
Each event package defines what its NOTIFY bodies contain and when the notifier sends them. These five cover most real deployments:
| Event | What it reports | Body | Typical use |
|---|---|---|---|
presence |
Whether a person is available | application/pidf+xml (PIDFPIDF. Presence Information Data Format (RFC 3863), the XML document that carries presence: a tuple with a basic status of open or closed.) |
Buddy lists in softphones and UC clients |
dialog |
The calls of a user: trying, early, confirmed, terminated | application/dialog-info+xml |
busy lamp fieldbusy lamp field. The lamps on a phone that show whether colleagues are idle, ringing, or busy, usually through the dialog event package. keys, call pickup (Module 22) |
message-summary |
New and old voicemail | application/simple-message-summary |
The message waiting indicatormessage waiting indicator. The lamp or icon on a phone that shows new voicemail, through the message-summary event package. lamp |
reg |
The bindings of an AOR | application/reginfo+xml |
IMS phones learn when the network ends their registration |
refer |
The progress of a REFER | message/sipfrag |
Call transfer (Module 22); the REFER itself creates the subscription |
Change Package in the builder to see each one. The dialog package drives the busy lamps on a receptionist’s phone:
Flow builder · a busy lamp for Bob
Dialog (busy lamp): Alice unsubscribes
- SIP
Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
All steps as text
- Alice → PBX: SUBSCRIBE. Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
- PBX → Alice: 200 OK. The notifier accepts the subscription. The 200 OK must carry Expires: the duration that counts.
- PBX → Alice: NOTIFY (active). The first NOTIFY follows at once, with the full current state.
- Alice → PBX: 200 OK (NOTIFY). Alice's phone confirms the NOTIFY.
- PBX → Alice: NOTIFY (active). Bob answers a call. The PBX reports a confirmed dialog, so Alice's busy lamp for Bob turns red.
- Alice → PBX: 200 OK (NOTIFY). Alice's phone confirms, and shows the new state.
- Alice → PBX: SUBSCRIBE (Expires: 0). Alice's phone unsubscribes: a SUBSCRIBE in the same dialog with Expires: 0.
- PBX → Alice: 200 OK. The notifier accepts. The subscription now ends.
- PBX → Alice: NOTIFY (terminated). A final NOTIFY says terminated, with the state one last time.
- Alice → PBX: 200 OK (NOTIFY). Alice's phone confirms. The subscription is over.
“Notifications are generated for the dialog package when an INVITE request is sent, when a new dialog comes into existence at a UA, or when the state or characteristics of an existing dialog changes.”Read the section ↗
The message-summary body is plain text, so even a phone with no display can read it:
“The text format begins with a simple status line, and optionally a summary line per message-context-class.”Read the section ↗
The reg package lets a phone learn that the network has shortened or ended its registration, for example to force it to authenticate again:
“To do this, the device subscribes to the registration event package for the address-of-record that it is registering contacts against.”Read the section ↗
A notifier that does not know the package answers 489 Bad Event. A UA lists the packages it supports in Allow-Events. Some voicemail systems send message-summary NOTIFYs with no SUBSCRIBE at all (“unsolicited NOTIFY”). Many phones accept them, but RFC 6665 does not define them; a SUBSCRIBE is the standard way.
23.3 PUBLISH and presence servers
Presence has three roles. The presentitypresentity. The person or resource whose presence others watch. In Module 23, Bob. (Bob) has a state; watcherswatcher. A subscriber to the presence of a presentity. In Module 23, Alice. (Alice) subscribe to it; the Presence serverPresence server. The server that collects presence state with PUBLISH and sends it to watchers with NOTIFY. sits between them. Bob’s phone does not answer every watcher itself. It publishes its state to the presence server, and the server notifies the watchers (RFC 3903):
“In this event package, the body of the notification contains a presence document. This document describes the presence of the presentity that was subscribed to.”Read the section ↗
Call flow · one publication: create, refresh, modify, remove
PUBLISH: create, refresh, modify, remove
- SIP
Bob's phone publishes his presence. An initial PUBLISH has a body and no SIP-If-Match.
All steps as text
- Bob → Presence server: PUBLISH (initial). Bob's phone publishes his presence. An initial PUBLISH has a body and no SIP-If-Match.
- Presence server → Bob: 200 OK. The presence server stores the state for an hour. SIP-ETag names it.
- Bob → Presence server: PUBLISH (refresh). Before the hour ends, Bob's phone refreshes. A refresh has SIP-If-Match and no body.
- Presence server → Bob: 200 OK. The server extends the state, and gives it a new entity-tag. The phone must use the new one next time.
- Bob → Presence server: PUBLISH (modify). Bob starts a call. His phone sends the new state, with SIP-If-Match and a body.
- Presence server → Bob: 200 OK. The server replaces the state and notifies every watcher of Bob.
- Bob → Presence server: PUBLISH (remove). Bob logs out. Expires: 0 removes the published state.
- Presence server → Bob: 200 OK. The server removes the state. Watchers now see what the server composes without it.
A publication is soft state too. The server names it with an entity-tagentity-tag. The name that a presence server gives to published state, in SIP-ETag. A later PUBLISH names the state it refreshes or changes in SIP-If-Match., returned in SIP-ETag. Every later PUBLISH names the state it acts on in SIP-If-Match:
| PUBLISH | SIP-If-Match | Body | Effect |
|---|---|---|---|
| Initial | None | The full state | Creates new state, and a new entity-tag |
| Refresh | The current entity-tag | None | Extends the lifetime, and changes the entity-tag |
| Modify | The current entity-tag | The new state | Replaces the state |
| Remove | The current entity-tag | None, with Expires: 0 | Deletes the state |
“To modify event state, the EPA MUST construct a PUBLISH request that includes in a SIP-If-Match header field the entity-tag of the event state publication to be modified.”Read the section ↗
The presence server does more than relay: it composes state from several publishers (Bob’s desk phone, his mobile, his calendar), and it applies Bob’s authorisation rules. A watcher that Bob has not yet allowed gets pending until he decides:
Flow builder · presence, with a pending watcher
Presence: Alice unsubscribes
- SIP
Bob's phone publishes his presence to the presence server: a PIDF document, with no SIP-If-Match.
All steps as text
- Bob → Presence server: PUBLISH. Bob's phone publishes his presence to the presence server: a PIDF document, with no SIP-If-Match.
- Presence server → Bob: 200 OK. The server stores the state. SIP-ETag names it, for later changes.
- Alice → Presence server: SUBSCRIBE. Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
- Presence server → Alice: 200 OK. The notifier accepts the subscription. The 200 OK must carry Expires: the duration that counts.
- Presence server → Alice: NOTIFY (pending). Bob has not yet said whether Alice may watch him. The NOTIFY says pending, with no state.
- Alice → Presence server: 200 OK (NOTIFY). Alice's phone confirms the NOTIFY.
- Presence server → Alice: NOTIFY (active). Bob allows Alice to watch him. The next NOTIFY says active, with his current state.
- Alice → Presence server: 200 OK (NOTIFY). Alice's phone confirms the NOTIFY.
- Bob → Presence server: PUBLISH. Bob's phone publishes the change. SIP-If-Match names the state it replaces.
- Presence server → Bob: 200 OK. The server replaces state dx200xyz. The new SIP-ETag is kwj449x.
- Presence server → Alice: NOTIFY (active). Bob starts a call. The presence server has his new state from his PUBLISH.
- Alice → Presence server: 200 OK (NOTIFY). Alice's phone confirms, and shows the new state.
- Alice → Presence server: SUBSCRIBE (Expires: 0). Alice's phone unsubscribes: a SUBSCRIBE in the same dialog with Expires: 0.
- Presence server → Alice: 200 OK. The notifier accepts. The subscription now ends.
- Presence server → Alice: NOTIFY (terminated). A final NOTIFY says terminated, with the state one last time.
- Alice → Presence server: 200 OK (NOTIFY). Alice's phone confirms. The subscription is over.
“If the "Subscription-State" value is "pending", the subscription has been received by the notifier, but there is insufficient policy information to grant or deny the subscription yet.”Read the section ↗
23.4 MESSAGE (page-mode instant messages)
MESSAGE (RFC 3428) carries an instant message in its body, usually text/plain. Each message is a separate transaction, routed like any other request; there is no dialog and no session. This is “page mode”: like a pager, one message at a time.
“MESSAGE requests do not initiate dialogs. User Agents MUST NOT insert Contact header fields into MESSAGE requests.”Read the section ↗
Call flow · an instant message and a reply
MESSAGE: an instant message and a reply
- SIP
Alice sends Bob an instant message. A MESSAGE has a text/plain body, and no Contact: it makes no dialog.
All steps as text
- Alice → Proxy B: MESSAGE. Alice sends Bob an instant message. A MESSAGE has a text/plain body, and no Contact: it makes no dialog.
- Proxy B → Bob: MESSAGE. Proxy B finds Bob's Contact and forwards the MESSAGE, like any request.
- Bob → Proxy B: 200 OK. Bob's phone shows the message and answers 200 OK.
- Proxy B → Alice: 200 OK. Alice's phone knows that the message arrived, not that Bob has read it.
- Bob → Alice: MESSAGE. Bob replies. The reply is a new request, with a new Call-ID: there is no conversation in SIP itself.
- Alice → Bob: 200 OK. Alice's phone confirms.
The response says how far the message got. A 200 OK means that the recipient’s UA has it, not that a person has read it. A 202 Accepted means that a store-and-forward server has it and will try to deliver it later:
“If the UAC receives a 202 Accepted response, the message has been delivered to a gateway, store and forward server, or some other service that may eventually deliver the message.”Read the section ↗
Call flow · stored by the IM service, delivered later
MESSAGE: stored and forwarded later
- SIP
Alice sends Bob a message while his phone is off.
All steps as text
- Alice → Proxy B: MESSAGE. Alice sends Bob a message while his phone is off.
- Proxy B → Alice: 202 Accepted. Proxy B's IM service stores the message. 202 means: accepted for later delivery, not delivered.
- Proxy B → Bob: MESSAGE. Later, Bob's phone registers again. The IM service sends the stored message, as a new request of its own.
- Bob → Proxy B: 200 OK. Bob's phone shows the message. Alice is not told: a 202 gives no delivery report.
MESSAGE is for short texts. A longer conversation, file transfer, or anything over 1300 bytes belongs in a session: an INVITE that sets up an MSRP media stream (RFC 4975), which carries messages over TCP. Delivery and read reports for MESSAGE need another extension, IMDN (RFC 5438).
“The size of MESSAGE requests outside of a media session MUST NOT exceed 1300 bytes, unless the UAC has positive knowledge that the message will not traverse a congestion-unsafe link at any hop, or that the message size is at least 200 bytes less than the lowest MTU value found en route to the UAS.”Read the section ↗
Common mistakes
Busy lamps that go dark after an hour
A receptionist’s phone subscribes to the dialog state of thirty colleagues, with Expires: 3600, and never refreshes. Everything works after a reboot. An hour later, each subscription ends with reason=timeout, and the lamps stay off: the receptionist transfers calls to people who are already on the phone.
Broken: never refreshes
Dialog (busy lamp): the subscription times out
- SIP
- ⚠Problem
Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
All steps as text
- Alice → PBX: SUBSCRIBE. Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
- PBX → Alice: 200 OK. The notifier accepts the subscription. The 200 OK must carry Expires: the duration that counts.
- PBX → Alice: NOTIFY (active). The first NOTIFY follows at once, with the full current state.
- Alice → PBX: 200 OK (NOTIFY). Alice's phone confirms the NOTIFY.
- PBX → Alice: NOTIFY (active). Bob answers a call. The PBX reports a confirmed dialog, so Alice's busy lamp for Bob turns red.
- Alice → PBX: 200 OK (NOTIFY). Alice's phone confirms, and shows the new state.
- PBX → Alice: NOTIFY (terminated). Alice's phone never refreshed, and the hour has passed. The notifier ends the subscription. Problem: From now on, Alice sees no changes. Her phone must subscribe again.
- Alice → PBX: 200 OK (NOTIFY). Alice's phone confirms. It hears about no further changes.
Remember: refresh every subscription before the Expires in the 200 OK runs out, and earlier if a NOTIFY gives a shorter expires. After terminated;reason=timeout, subscribe again with a new SUBSCRIBE.
Rejecting a NOTIFY that arrives before the 200 OK
A phone creates the subscription dialog only when the 200 OK to its SUBSCRIBE arrives. On a network with a little packet loss, the first NOTIFY sometimes arrives first. The phone finds no dialog, answers 481, and so ends the subscription it has just created. The fault comes and goes, and nobody can reproduce it in the lab.
Broken
Broken: the first NOTIFY gets 481
- SIP
- ⚠Problem
Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
All steps as text
- Alice → PBX: SUBSCRIBE. Alice's phone subscribes to Bob's state. Event names the package; Expires asks for an hour.
- PBX → Alice: NOTIFY (active). The first NOTIFY overtakes the 200 OK, which a packet loss delays.
- Alice → PBX: 481 Call/Transaction Does Not Exist. Alice's phone has no dialog yet, so it rejects the NOTIFY with 481. Problem: A subscriber must accept a NOTIFY that arrives before the 200 OK. The 481 ends the subscription.
- PBX → Alice: 200 OK. The notifier accepts the subscription. The 200 OK must carry Expires: the duration that counts.
Remember: a subscriber must accept a NOTIFY that matches a SUBSCRIBE it has sent (same Call-ID, its own tag, same Event), even before the 200 OK. The course’s checker calls this rule notify-early.
Treating a 412 to PUBLISH as a passing error
A softphone refreshes its presence with the stored entity-tag. After a laptop sleeps for longer than the publication lasted, the server has forgotten that state and answers 412 Conditional Request Failed. The softphone keeps refreshing the same entity-tag, gets 412 each time, and the user looks offline all day.
Problem: refreshes the old state
Problem: the refresh fails with 412, and Bob stays offline
- SIP
- ⚠Problem
Bob's laptop publishes his presence for an hour.
All steps as text
- Bob → Presence server: PUBLISH (initial). Bob's laptop publishes his presence for an hour.
- Presence server → Bob: 200 OK. The server stores the state as entity-tag a7f3c1.
- Presence server → Alice: NOTIFY (open). Alice watches Bob. Her phone shows him as available.
- Alice → Presence server: 200 OK (NOTIFY). Alice's phone updates Bob's icon.
- Presence server → Alice: NOTIFY (closed). Bob's laptop sleeps for two hours. His publication expires, so the server reports him as offline.
- Alice → Presence server: 200 OK (NOTIFY). Alice's phone updates Bob's icon.
- Bob → Presence server: PUBLISH (refresh). The laptop wakes up and refreshes with its stored entity-tag, which no longer exists.
- Presence server → Bob: 412 Conditional Request Failed. The server has no state with that entity-tag, so the refresh fails.
- Bob → Presence server: PUBLISH (refresh). Later, the laptop tries to refresh again, with the same old entity-tag. Problem: The phone treats the 412 as a passing error. Bob looks offline to everybody, though he is at his desk.
- Presence server → Bob: 412 Conditional Request Failed. The same 412, every time. The phone never sends its state again.
“If there is no matching event state, e.g., the event state to be refreshed has already expired, the EPA receives a 412 (Conditional Request Failed) response to the PUBLISH request.”Read the section ↗
Remember: after a 412, drop the entity-tag and send a new initial PUBLISH with the full state.
A large MESSAGE over UDP
A chat client sends long messages, such as pasted logs, as MESSAGE over UDP. Above about 1500 bytes the IP layer fragments them. On a network that drops fragments, as many firewalls and NAT routers do, those messages simply never arrive, and the sender gets no error.
Broken
Broken: a 1.5 KB MESSAGE over UDP
- SIP
- ⚠Problem
Alice pastes her meeting notes into a message. Her phone sends all 1.5 KB over UDP.
Above 1300 bytes, UDP splits the packet into IP fragments. If the network drops one fragment, the whole message never arrives.
All steps as text
- Alice → Proxy B: MESSAGE. Alice pastes her meeting notes into a message. Her phone sends all 1.5 KB over UDP. Problem: Above 1300 bytes, UDP splits the packet into IP fragments. If the network drops one fragment, the whole message never arrives.
- Proxy B → Bob: MESSAGE. Proxy B forwards it over UDP as well, in fragments.
- Bob → Proxy B: 200 OK. Bob's phone shows the message.
- Proxy B → Alice: 200 OK. This time every fragment arrived. On a busy network, many such messages vanish.
Remember: keep MESSAGE under 1300 bytes, send it over TCP or TLS, or use an MSRP session for long content. The course’s checker calls this rule udp-size (Module 19).
Summary
- SUBSCRIBE asks for notifications; NOTIFY carries the state. Event names the package, Expires sets the duration, and Subscription-State says active, pending, or terminated, with a reason.
- The notifier sends a NOTIFY at once after accepting a subscription, and after every change. The first NOTIFY can arrive before the 200 OK.
- Subscriptions are soft state: refresh before Expires, unsubscribe with Expires: 0.
- Common packages: presence (PIDF), dialog (busy lamps, pickup), message-summary (voicemail lamp), reg (registration state), and refer (transfer progress).
- PUBLISH sends state to a presence server; SIP-ETag and SIP-If-Match name the state to refresh, change, or remove. A 412 means: publish the full state again.
- MESSAGE sends one instant message, with no dialog. 200 means delivered to the UA; 202 means stored for later. Keep it under 1300 bytes, or use MSRP.