jens-maus / node-unifi

NodeJS class for querying/controlling a UniFi-Controller (UDM-Pro, UDM-SE, UDM, UDR, UDW, CloudKey Gen1/Gen2) from Ubiquiti (www.ui.com)
MIT License
140 stars 50 forks source link

allow to supply urlParams for WebSocket connection #217

Closed dersimn closed 10 months ago

dersimn commented 11 months ago

Allow to supply urlParams for WebSocket connection, one use-case is to enable client:sync messages that will update the listener in regular intervals about the current state of a client

const listenData = await unifi.listen({urlParams: {clients: 'v2'}});

unifi.on('client:sync.*', function (data) {
  console.log('client:sync', util.inspect(data, false, null));
});

Example output:

client:sync [
  {
    anomalies: 0,
    ap_mac: '<id stuff>',
    assoc_time: 1695517377,
    authorized: true,
    blocked: false,
    bssid: '<id stuff>',
    ccq: 844,
    channel: 6,
    channel_width: '<id stuff>',
    dhcpend_time: 50,
    display_name: 'PulseCounter_3AD5BD',
    essid: 'HMA-AP',
    fingerprint: { has_override: false },
    first_seen: 1652977405,
    hostname: 'PulseCounter_3AD5BD',
    id: '<id stuff>',
    idletime: 1,
    ip: '10.0.0.217',
    is_guest: false,
    is_wired: false,
    last_seen: 1695847617,
    latest_assoc_time: 1695517377,
    local_dns_record_enabled: false,
    mac: '<id stuff>',
    mimo: 'MIMO_1',
    noise: -110,
    noted: false,
    oui: 'Espressif Inc.',
    powersave_enabled: true,
    radio: 'ng',
    radio_name: 'wifi0',
    radio_proto: 'ng',
    rate_imbalance: -91,
    rssi: 21,
    rx_bytes: 1608743,
    'rx_bytes-r': 77,
    rx_packets: 46390,
    rx_rate: 6000,
    signal: -75,
    site_id: '<id stuff>',
    status: 'online',
    tx_bytes: 142733,
    'tx_bytes-r': 8,
    tx_mcs_index: 7,
    tx_packets: 2837,
    tx_rate: 72222,
    type: 'WIRELESS',
    unifi_device: false,
    uplink_mac: '<id stuff>',
    uptime: 330240,
    usage_bytes: 5280682.16969697,
    use_fixedip: false,
    user_id: '<id stuff>',
    usergroup_id: '',
    virtual_network_override_enabled: false,
    wifi_experience_average: 100,
    wifi_experience_score: 100,
    wifi_tx_attempts: 4243,
    wlanconf_id: '<id stuff>'
  },
  { message: 'client:sync', rc: 'ok' }
]
jens-maus commented 11 months ago

First of all, thanks for your contribution. But can you please explain your proposed change with a bit more details please. Why is it necessary to set this clients v2 url parameter? And also please explain what might be other potential use cases for this urlParams parameter? And why would one set or not set this clients v2 Parameter and not simply send it always in newer versions of node-unifi?

dersimn commented 11 months ago

tbh, I don't know if there are other options than this client=v2.
If you use the web inspector on the latest UniFi Console, it's one of the options parameters that the official WebUI uses (in my case it is: wss://192.168.2.113:8443/wss/s/default/events?clients=v2&critical_notifications=true).

If you specify this parameter you'll receive this client:sync events every few seconds, if you leave the option away the WebSocket is rather silent. I'd like to receive these messages as some kind of heartbeat signal that a client is still online. (If you listen to the disconnect events that shouldn't be necessary, but in my experience these UniFi Events are never 100% reliable.)