% subscribe.m
redis = py.importlib.import_module('redis');
R = redis.Redis(pyargs('host','localhost','port',int64(12987),...
                       'decode_responses',py.True));
try
  R.ping();
  fprintf('Connected to server.\n')
catch EO
  fprintf('Is Redis running?  Unable to connect: %s\n', EO.message)
end

% modify configuration to enable keyspace notification
R.config_set('notify-keyspace-events', 'KEA');

Sub = R.pubsub();
keys_to_match = "sensor_*_temperature";
Sub.psubscribe("__keyspace@0__:" + keys_to_match);

while 1
   try
       message = Sub.get_message();
   catch EO
       fprintf('lost connection to server: %s\n', EO.message)
       break
   end
   if message == py.None
       pause(0.1)
       continue
   end
   keyname = replace(message{'channel'}, '__keyspace@0__:','');
   if keyname == keys_to_match
       % initial subscription value
       continue
   end
   % gets here if any of the matching keys was updated
   value = string(R.get(keyname));
   fprintf('%s = %s\n', string(keyname), value)
end
