Subjects and Publish-Subscribe


Just as projects restrict the boundaries of where messages are sent, subjects partition the flow of messages within a project. A subject is a logical message address that can be thought of as providing a virtual connection between RTclients. Subjects allow an RTclient to publish a message to multiple processes with a single operation. Subjects are designated by a name, which can be any character string with a few restrictions.

A message in TIBCO SmartSockets has both a sender property and a destination property. (See the section on message composition in the messages chapter of the TIBCO SmartSockets User’s Guide for a full discussion of message properties.) Subjects are used for the sender and destination properties. When an RTclient subscribes to a subject, it gets any published messages whose destination property is set to that subject. Think of this as the process signing up for messages sent to the subject. For example, in a network monitoring application, you might partition messages by the types of things being monitored — routers, bridges, switches, and so on. These areas are declared as subjects such as /router, /bridge, /switch. All messages pertaining to routers are constructed with the /router subject as its destination property. Any RTclient interested in receiving messages destined for /router subscribes to the /router subject. This is also known as the publish-subscribe paradigm because RTclients publish messages to specific subjects, and RTclients also subscribe to subjects in which they are interested.

The TIBCO SmartSockets publish-subscribe communications model allows an RTclient to simply publish the message with a subject as the destination property, and RTserver takes care of routing the message to all RTclients that are subscribed to that subject. RTclient can subscribe or unsubscribe to a subject at any time, which allows RTclient to control the quantity of incoming messages. TIBCO SmartSockets applications can be easily reconfigured when hardware resources are added or removed, without changing a single line of code.

Hierarchical Subject Namespace

To provide greater flexibility and scalability for large applications, TIBCO SmartSockets subject names are arranged in a hierarchical namespace much like UNIX file names or World Wide Web URLs. This hierarchical namespace allows for large numbers of subject names to be created with similar but non-conflicting names, such as /stocks/NYSE/computer and /stocks/NASDAQ/gold, and also for many powerful operations, such as publish-subscribe with wildcards, to be performed. Small TIBCO SmartSockets systems can be easily built without a great deal of complexity, and large systems can also be more easily built with these hierarchical subject names.

A hierarchical subject name consists of components laid out left-to-right separated by slashes (/). Each component can contain any other non-slash characters except an asterisk (*) and the ellipsis sequence (...), both of which are used for wildcards in publish-subscribe. Examples of hierarchical subject names include /system, /system/primary/eps, /system/backup/eps and /nodes/workstation.tibco.com/support. The hierarchy can be specified to any number of levels. For more details, see the section on hierarchical subject names in the publish-subscribe chapter of the TIBCO SmartSockets User’s Guide.

Specifying Wildcards in Subjects

When subscribing or publishing to a subject, wildcards can be used in the specification of the subject name to match multiple subjects. Using wildcards ("*" or "...") in subjects is much like using wildcards for file names in an operating system command line. The wildcard "*" operates much like it does on Windows, UNIX, or OpenVMS. It can be used for an entire subject name component or as part of a more complicated wildcard containing other characters, such as "foo*bar". A wildcard component using "*" never matches more than one component; for example, "foo*bar" does not match "foo/bar".

The wildcard "..." operates much like it does on OpenVMS, where it matches any number of levels, including zero levels, of components. It must be used as an entire component; for example, "auto..." is not a wildcard. Multiple wildcards can be used in a subject name, such as /a*b*/.../d. For more details, see the section on using wildcards with subjects in the publish-subscribe chapter of the TIBCO SmartSockets User’s Guide.

Message Routing Example

This section provides an example of how a message, originating from a single RTclient, is published to all RTclients subscribed to a subject. In Figure 4, processes are represented by circles and connections between processes are represented by dark lines. As you can see, there is a single sending process (Send), and two receivers (Receive1, Receive2). Each of these RTclients is connected to the same RTserver. The Receive1 and Receive2 programs have both subscribed to the /sub1 subject. If the Send process wants to publish a message to the /sub1 subject, this sequence of events occurs:

  1. The Send program constructs a message with /sub1 as the destination.
  1. The Send program publishes the message.
  2. RTserver receives the message.
  3. RTserver looks at the destination (/sub1) of the message.
  4. RTserver publishes the message to all RTclients currently subscribed to the /sub1 subject.
  5. Each RTclient, Receive1 and Receive2, receive a copy of the message.

Figure 4 shows this flow of the message through RTserver. Note that if the Send program was also subscribed to the /sub1 subject, it too would have received a copy of the message from RTserver.

Figure 4 RTserver Message Routing

Demonstrating Publish-Subscribe Services

Let’s go back to the sending and receiving examples earlier in this lesson. Line 9 of the sending program calls TipcSrvMsgWrite:

TipcSrvMsgWrite("/tutorial/lesson2", TipcMtLookupByNum(T_MT_INFO), 
                  TRUE, T_IPC_FT_STR, "Hello World!", NULL); 

The first argument to the function ("/tutorial/lesson2") is the subject the message is being published to. For the receiving program to get the message, line 11 of the receiving program is required:

TipcSrvSubjectSetSubscribe("/tutorial/lesson2", TRUE); 

This line causes the receiving program to start receiving any messages published to the /tutorial/lesson2 subject.

Step 10

Start the receiving program

To illustrate this concept more clearly, you need three separate windows on your workstation. In the first window, start up the receiving program:

UNIX:
$ receive.x 
OpenVMS:
$ run receive 
Windows:
$ receive 

Step 11

Edit the receive.c program to subscribe to subject foo

Now, edit the receive.c program to change line 11 so it subscribes to the /smartsockets/foo subject. Line 11 should now look like this:

TipcSrvSubjectSetSubscribe("/smartsockets/foo", TRUE); 

Save your changes to receive.c.

Step 12

Compile and link the receiving program

Compile and link the receiving program.

Step 13

Start the new receiving program

In a second window start up the new receiving program:

UNIX:
$ receive.x 
OpenVMS:
$ run receive 
Windows:
$ receive 

Step 14

Start the sending program

Finally, in the third window, start up the sending program:

UNIX:
$ send.x 
OpenVMS:
$ run send 
Windows:
$ send 

You should see that your original receiving program got the message and printed it out.

Text from INFO message = Hello World! 

Notice that the new receiving program (that had subscribed to the /smartsockets/foo subject) did not receive the message. In fact, it is still blocked, waiting for a message. Enter Ctrl-c to stop execution and return to the operating system prompt.

Step 15

Edit the receiving program to receive a new subject

To see how one message can be delivered to two processes in a single operation, go back and edit the receiving program to again receive the /tutorial/lesson2 subject. Line 11 should now look like this:

TipcSrvSubjectSetSubscribe("/tutorial/lesson2", TRUE); 

Step 16

Compile and link the receiving program

Compile and link the receiving program as before.

Step 17

Start the receiving program

Now start the receiving program in the first window:

UNIX:
$ receive.x 
OpenVMS:
$ run receive 
Windows:
$ receive 

Step 18

Start a second instance of the receiving program

In the second window, start up a second instance of the same receiving program:

UNIX:
$ receive.x 
OpenVMS:
$ run receive 
Windows:
$ receive 

Step 19

Start the sending program

Finally, in the third window, start up the sending program:

UNIX:
$ send.x 
OpenVMS:
$ run send 
Windows:
$ send 

You should see this display in both windows where the receiving programs are running:

Text from INFO message = Hello World! 

If you wish, you can start any number of receiving programs, run the sending program, and watch the message get delivered to all of them with a single operation.

Note that you did not have to change a single line of code in the sending program to have access to this feature. The ability to send a message to multiple processes with a single operation, without having to specify the location of the processes, is a key feature of TIBCO SmartSockets and makes the testing, debugging, and maintenance of your network application much easier. Also, through the use of subjects and TIBCO SmartSockets publish-subscribe services, you achieve location transparency. This implies that your programs can be easily relocated anywhere on your network without changing a single line of code.


TIBCO SmartSockets™ Tutorial
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com