How can I get a list of all connected/logged in users?
You can try
// 3600s is one hour.
$uids = Drupal::database()-&gt;query('SELECT uid FROM sessions WHERE uid != 0 AND `timestamp` &gt;=:time',[':time'=&gt;(Drupal::time()-&gt;getCurrentTime()-3600)])-&gt;fetchCol();