Protocol Demo

    On the wire you can publish and subscribe using a simple set of protocol commands.

    Instructions

    1. Open a terminal session.

    You’ll use this terminal as the subscriber.

    2. Connect to NATS.

    Expected result:

    1. Connected to demo.nats.io.
    2. Escape character is '^]'.
    3. INFO {"server_id":"NCXMJZYQEWUDJFLYLSTTE745I2WUNCVG3LJJ3NRKSFJXEG6RGK7753DJ","version":"2.0.0","proto":1,"go":"go1.11.10","host":"0.0.0.0","port":4222,"max_payload":1048576,"client_id":5089}

    3. Run the subscriber.

    Subscribe to the wildcard subject foo.* with subject ID of 90.

    1. sub foo.* 90

    Subscriber result: +OK indicating successful interest registration.

    1. sub foo.* 90

    You’ll use this terminal for the publisher.

    5. Connect to NATS.

    1. telnet demo.nats.io 4222

    Expected result:

    6. Publish a message.

    The message includes the command (pub), subject (foo.bar), and length of the payload (5). Press enter and provide the payload (), then press enter again.

    1. pub foo.bar 5
    2. hello

    Publisher result: +OK indicating message publication.

    1. pub foo.bar 5
    2. hello
    3. +OK

    Subscriber result: MSG + subject name + subscription ID + message payload size + message payload hello.

    1. sub foo.* 90
    2. +OK
    3. hello

    7. Publish another message with reply subject.

    1. pub foo.bar optional.reply.subject 5
    2. +OK

    8. Unsubscribe from interest in the subject.

    You can use the UNSUB command to unsubscribe from a message.

    Run the subscriber to unsubscribe:

    1. unsub 90

    Subscriber result: +OK indicating successful deregistration of interest.

    1. unsub 90
    2. +OK

    9. Reconnect to server and subscribe.

    1. telnet demo.nats.io 4222

      10. Explore the ping/pong interval.

      If you leave your telnet session open for a few minutes, you may notice that your clients receives ping requests from the server. If your client is not active, or does not respond to the server pings within the ping/pong interval, the server disconnects the client. The error message is -ERR 'Stale Connection'.

      You can send a ping request to the serve and receive a PONG reply. For example: