Developer Zone

Advanced Software Development with MATLAB

Notifications from MATLAB

Waiting for a computer or cluster to finish a task is about as exciting as watching paint dry. XKCD even has a comic about it.

XKCD

MATLAB developers often deal with heavy computational loads, often running for long periods of time and involving lots of data. There are many situations where I stand up and stretch or perhaps get myself another refill of coffee, a beverage that I love almost to a fault.

A system for notifying me of progress, completion or errors, would allow me to step away from the my screen and return efficiently. Wouldn't it be nice if I could route that notification to kick off the next task? Or let the entire team know the status of a build or test activity. That is where the AWS Simple Notification System (SNS) comes in.

In this post, I will describe how MathWorks developers can leverage cloud capabilities and quickly setup a fully managed publication / subscription (pub/sub) messaging system. Once configured, it would enable push based messaging from MATLAB.

The code behind this post has been released on github.com and once the tooling as been setup as per the installation instructions, it becomes trivial to define fan-out topologies and set up MATLAB to handle the notifications for myself as well as entire teams.

As an example, MATLAB offers a performance benchmark function called bench. Though this is a contrived workload let me instruct it to run the performance benchmark 200 times. On my local computer, this is a test that takes about 20 mins. Just enough time for me to get a nice cup of coffee and stretch as MATLAB does the heavy lifting.

bench(200);

Setup of the topic:

First, let me setup a topic on SNS. This is one-time operation. A topic defines a logical address point which acts as communication channel. It allows me to group multiple endpoints. These endpoints can be simple like email and SMS or more complex like HTTP(s) calls to servers, AWS Lambda functions or Queues.

sns = aws.sns.Client();
sns.initialize;
topic = sns.createTopic('coffee-notification');

Each topic comes with a unique identifier called an ARN.

This is easily queried:

topicARN = topic.getTopicArn;

topicARN =

    'arn:aws:sns:us-west-2:74[REDACTED]02:coffee-notification'

Subscription of the endpoints:

For illustration, let us assume that I wish MATLAB to send me a text message AND send me an email when my benchmarks are complete. This is setup in a few lines of code. This, again, is a one-time operation as I define the topology of which endpoints to subscribe to my topic.

sns.subscribe(topicARN,'email','ah****ra@mathworks.com');
sns.subscribe(topicARN,'sms','+12485555555');

In a nutshell, what we have done so far, is subscribed my email and cellphone to the topic we created. A lot more is possible but let me keep this illustration simple.

Send a Notification:

Sending a notification to all my subscribed endpoints is accomplished with a single line of MATLAB code. My instrumented benchmark code now looks like:

bench(200);
sns.publish(topicARN, 'Benchmark complete!');

Hit run!... and now to go get that cup of coffee. 🙂

Sure enough, 20 minutes later, I get notified on my cellphone and email:

SMS

Ok, the use of this tooling as a coffee break notifier is a trivial use-case to illustrate a point. Tooling such as this gets particularly useful when building bigger systems where the notification can trigger downstream actions such as generation of reports, etc.

The functionality really begins to shine in use-cases where the notification system needs to scale to hundreds or thousands of subscriber endpoints. These notifications can be consumed by humans and other apps/services alike. This pub-sub pattern is common in building service oriented systems where it is used to interconnect different technology stacks.

So, what are you planning to do with your MATLAB? Besides letting it call you back from your coffee break?




Published with MATLAB® R2019b

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.