Use WebSockets to listen to status notifications

You can use WebSockets to listen to notifications about the status of monitored elements.

Steps

  1. Login to the SMC API with the login method.
  2. Transfer the session cookie to the WebSocket session or share the socket itself.
  3. Connect to the WebSocket using the following URL:
    ws://[server]:[port]/[version]/notification/socket
  4. Send one of the following types of commands to listen for notifications:
    • To open a channel to subscribe to notifications for all elements, enter:
      {"context":"*"}

      The channel is identified by its subscription_id value.

    • To open a channel to listen to a specific type of element, enter:
      {"context":"RelName"}

      The command opens of a channel dedicated to the element associated with the RelName. You can use a list of RelNames separated by a comma followed by a space (, ) in the command:

      Example: "host, router"

      The list of RelNames can be found from the link http://127.0.0.1:8082/7.0/api.

  5. To unsubscribe from status notifications, send the following query:
    {"unsubscribe":{id_of_the_previous_subscription}}

Example

For example, if you send the following commands:

{"context":"*"}  
{"context":"host"}

You receive one notification on the channel with "subscription_id":0 listening to all elements and one notification the channel with "subscription_id":1 dedicated to of hosts elements. The creation of a host sends a notification on both channels. The creation of a router only sends a notification on the "subscription_id":0 channel.

>>>{"event":{"type":"create","element":"http://localhost:8082/7.0/elements/router/1733"},"subscription_id":0}
>>>{"event":{"type":"create","element":"http://localhost:8082/7.0/elements/host/1732"},"subscription_id":0}
>>>{"event":{"type":"create","element":"http://localhost:8082/7.0/elements/host/1732"},"subscription_id":1}
>>>{"event":{"type":"update","element":"http://localhost:8082/7.0/elements/admin_user/3"},"subscription_id":0}
>>>{"event":{"type":"update","element":"http://localhost:8082/7.0/elements/admin_user/3"},"subscription_id":0}