{"id":2248,"date":"2019-10-14T17:58:57","date_gmt":"2019-10-14T21:58:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/developer\/?p=2248"},"modified":"2019-10-14T17:58:57","modified_gmt":"2019-10-14T21:58:57","slug":"notifications-from-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/developer\/2019\/10\/14\/notifications-from-matlab\/","title":{"rendered":"Notifications from MATLAB"},"content":{"rendered":"<div class=\"content\"><p>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.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/imgs.xkcd.com\/comics\/compiling.png\" alt=\"XKCD\"> <\/p><p>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.<\/p><p>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 <a href=\"https:\/\/aws.amazon.com\/sns\/\" rel=\"noopener\" target=\"_blank\">AWS Simple Notification System (SNS)<\/a> comes in.<\/p><p>In this post, I will describe how MathWorks developers can <a href=\"https:\/\/blogs.mathworks.com\/developer\/2018\/01\/15\/leveraging-cloud-capabilities-from-matlab\/\" rel=\"noopener\" target=\"_blank\">leverage cloud capabilities<\/a> and quickly setup a fully managed publication \/ subscription (pub\/sub) messaging system. Once configured, it would enable push based messaging from MATLAB.<\/p><p>The code behind this post has been released on <a href=\"https:\/\/github.com\/mathworks-ref-arch\/matlab-aws-sns\"  rel=\"noopener\" target=\"_blank\">github.com<\/a> and once the tooling as been setup as per the <a href=\"https:\/\/github.com\/mathworks-ref-arch\/matlab-aws-sns\/blob\/master\/Documentation\/Installation.md\" rel=\"noopener\" target=\"_blank\">installation instructions<\/a>, it becomes trivial to define fan-out topologies and set up MATLAB to handle the notifications for myself as well as entire teams.<\/p><p>As an example, MATLAB offers a performance benchmark function called <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/bench.html\" rel=\"noopener\" target=\"_blank\">bench<\/a>. 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.<\/p><pre class=\"codeinput\">bench(200);\r\n<\/pre><p><b>Setup of the topic:<\/b><\/p><p>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.<\/p><pre class=\"codeinput\">sns = aws.sns.Client();\r\nsns.initialize;\r\ntopic = sns.createTopic(<span class=\"string\">'coffee-notification'<\/span>);\r\n<\/pre><p>Each topic comes with a unique identifier called an <a href=\"https:\/\/docs.aws.amazon.com\/general\/latest\/gr\/aws-arns-and-namespaces.html\"  rel=\"noopener\" target=\"_blank\">ARN<\/a>.<\/p><p>This is easily queried:<\/p><pre class=\"codeinput\">topicARN = topic.getTopicArn;\r\n\r\ntopicARN =\r\n\r\n    <span class=\"string\">'arn:aws:sns:us-west-2:74[REDACTED]02:coffee-notification'<\/span>\r\n<\/pre><p><b>Subscription of the endpoints:<\/b><\/p> <p>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.<\/p><pre class=\"codeinput\">sns.subscribe(topicARN,<span class=\"string\">'email'<\/span>,<span class=\"string\">'ah****ra@mathworks.com'<\/span>);\r\nsns.subscribe(topicARN,<span class=\"string\">'sms'<\/span>,<span class=\"string\">'+12485555555'<\/span>);\r\n<\/pre><p>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.<\/p><p><b>Send a Notification:<\/b><\/p><p>Sending a notification to all my subscribed endpoints is accomplished with a single line of MATLAB code. My instrumented benchmark code now looks like:<\/p><pre class=\"codeinput\" style=\"white-space: pre-line;\">bench(200);\r\nsns.publish(topicARN, <span class=\"string\">'Benchmark complete!'<\/span>);\r\n<\/pre><p>Hit run!... and now to go get that cup of coffee. \ud83d\ude42<\/p><p>Sure enough, 20 minutes later, I get notified on my cellphone and email:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/developer\/files\/CellPhoneNotification2.jpg\" alt=\"SMS\"> <\/p><p>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.<\/p><p>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.<\/p><p>So, what are you planning to do with your MATLAB? Besides letting it call you back from your coffee break?<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_e35183847bba4db1a8a419dbdf330328() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='e35183847bba4db1a8a419dbdf330328 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e35183847bba4db1a8a419dbdf330328';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_e35183847bba4db1a8a419dbdf330328()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2019b<br><\/p><\/div><!--\r\ne35183847bba4db1a8a419dbdf330328 ##### SOURCE BEGIN #####\r\n%%\r\n% *Let me know when you are done*\r\n% \r\n% Waiting for a computer or cluster to finish a task is about as exciting \r\n% as watching paint dry. XKCD even has a comic about it.\r\n%\r\n% <<https:\/\/imgs.xkcd.com\/comics\/compiling.png>>\r\n% \r\n% MATLAB developers often deal with heavy computational loads, often running\r\n% for long periods of time and involving lots of data. There are many \r\n% situations where I stand up and stretch or perhaps get myself another\r\n% refill of coffee, a beverage that I love almost to a fault.\r\n% \r\n% A system for notifying me of progress, completion or errors, would allow me\r\n% to step away from the my screen and return efficiently. Wouldn't it be nice\r\n% if I could route that notification to kick off the next task? Or let the entire \r\n% team know the status of a build or test activity. That is where the\r\n% <https:\/\/aws.amazon.com\/sns\/ AWS Simple Notification System (SNS)> comes in. \r\n% \r\n% In this post, I will describe capability for MathWorks developers to \r\n% leverage cloud capabilities and quickly setup a fully managed \r\n% publication \/ subscription (pub\/sub) messaging system. Once configured, \r\n% it would enable push based messaging from MATLAB.\r\n% \r\n% The code behind this post has been released on \r\n% <https:\/\/github.com\/mathworks-ref-arch\/matlab-aws-sns github.com> and\r\n% once the tooling as been setup as per the \r\n% <https:\/\/github.com\/mathworks-ref-arch\/matlab-aws-sns\/blob\/master\/Documentation\/Installation.md installation instructions>, \r\n% it becomes trivial to define fan-out topologies and \r\n% set up MATLAB to push notifications.\r\n% \r\n% As an example, MATLAB offers a performance benchmark function called \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/bench.html bench>. Though this \r\n% is a contrived workload let me instruct it to run the performance \r\n% benchmark 200 times. On my local computer, this is a test that takes about\r\n% 20 mins. Just enough time for me to get a nice cup of coffee and stretch as \r\n% MATLAB does the heavy lifting.\r\n\r\nbench(200);\r\n\r\n%%\r\n% *Setup of the topic*\r\n% First let me setup a topic on SNS. This is one-time operation. A topic\r\n% defines a logical address point which acts as communication channel. It \r\n% allows me to group multiple endpoints. These endpoints can be simple \r\n% like email and SMS or more complex like HTTP(s) calls to servers, \r\n% AWS Lambda functions or Queues. \r\n\r\nsns = aws.sns.Client();\r\nsns.initialize;\r\ntopic = sns.createTopic('coffee-notification');\r\n\r\n%%\r\n% Each topic comes with a unique identifier called \r\n% (https:\/\/docs.aws.amazon.com\/general\/latest\/gr\/aws-arns-and-namespaces.html\r\n% ARN).\r\n% \r\n% This is easily queried:\r\n\r\ntopicARN = topic.getTopicArn;\r\n\r\ntopicARN =\r\n\r\n    'arn:aws:sns:us-west-2:74[REDACTED]02:coffee-notification'\r\n    \r\n%% \r\n% *Subscription of the endpoints*\r\n% For illustration, let us assume that I wish MATLAB to send me a text\r\n% message AND send me an email when my benchmarks are complete. This is\r\n% setup in a few lines of code. This, again, is a one-time operation as I\r\n% define the topology of which endpoints to subscribe to my topic.\r\n\r\nsns.subscribe(topicARN,'email','ah****ra@mathworks.com');\r\nsns.subscribe(topicARN,'sms','+12485555555');\r\n\r\n%%\r\n% In a nutshell, what we have done so far, is subscribed my email and \r\n% cellphone to the topic we created. A lot more is possible but let me keep\r\n% this illustration simple.\r\n\r\n%%\r\n% *Send a Notification*\r\n% Sending a notification to all my subscribed endpoints is accomplished\r\n% with a single line of MATLAB code. My instrumented benchmark code now\r\n% looks like:\r\n% \r\n\r\nbench(200);\r\nsns.publish(topicARN, 'Benchmark complete');\r\n\r\n%%\r\n% Hit run!... and now to go get that cup of coffee. \ud83d\ude42\r\n%\r\n\r\n%% \r\n% Sure enough, 20 minutes later, I get notified on my cellphone and email:\r\n%\r\n% <<CellPhoneNotification.png>>\r\n%\r\n\r\n%%\r\n% Ok, the use of this tooling as a coffee break notifier is a trivial use-case\r\n% to illustrate a point. Tooling such as this gets particularly useful when \r\n% building bigger systems where the notification can trigger\r\n% downstream actions such as generation of reports, etc. \r\n% \r\n% The functionality really begins to shine in use-cases where the \r\n% notification system needs to scale to hundreds or thousands of subscriber \r\n% endpoints to be consumed by both humans and other software apps and \r\n% services alike. This pub-sub pattern is common in building service \r\n% oriented systems and used across different technology stacks. \r\n% \r\n% So, what are you planning to do with your MATLAB? Besides letting it call \r\n% you back from your coffee break? \r\n\r\n\r\n##### SOURCE END ##### e35183847bba4db1a8a419dbdf330328\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/developer\/files\/CellPhoneNotification2.jpg\" onError=\"this.style.display ='none';\" \/><\/div><p>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. MATLAB developers often deal with heavy computational loads, often... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/developer\/2019\/10\/14\/notifications-from-matlab\/\">read more >><\/a><\/p>","protected":false},"author":135,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,30,21,13,10],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/2248"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/users\/135"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/comments?post=2248"}],"version-history":[{"count":20,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/2248\/revisions"}],"predecessor-version":[{"id":2298,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/2248\/revisions\/2298"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media?parent=2248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/categories?post=2248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/tags?post=2248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}