Wolfram Computation Meets Knowledge

ChannelMessages

Contributed by: Richard Hennigan (Wolfram Research)

Send relevant data to a channel object when a message occurs

ResourceFunction["ChannelMessages"][eval,channel]

sends message data to channel when a message occurs during evaluation of eval.

ResourceFunction["ChannelMessages"][eval,{channel1,channel2,}]

sends message data to the channels channel1,channel2,.

Examples

Basic Examples

Listen to a channel for message data:

In[1]:=
listener = ChannelListen["MessageChannel", (message = ReleaseHold[#Message]) &]
Out[1]=

Send message data to the channel when messages occur:

In[2]:=
ResourceFunction["ChannelMessages"][1/0, "MessageChannel"]
Out[2]=

Print the message:

In[3]:=
message["Message"]

View the full data in a notebook:

In[4]:=
NotebookPut[ResourceFunction["AssociationNotebook"][message]]
Out[4]=
In[5]:=
CloudGet["https://www.wolframcloud.com/objects/d3f50537-c924-471b-ad10-0e9aafaf5d11"] (* Evaluate this cell to copy the example input from a cloud object *)

Applications

Redirect messages from a deployed APIFunction to the current session:

In[6]:=
ChannelListen[
  "MessageChannel", (SessionSubmit[
     ReleaseHold[#Message]["Message"]]) &];
In[7]:=
api = CloudDeploy[
  APIFunction[{"x" -> "Number", "y" -> "Number"}, ResourceFunction["ChannelMessages"][#x/#y, "MessageChannel"] &]]
Out[7]=

Messages from the API will print in the current session:

In[8]:=
URLExecute[api, {"x" -> 1, "y" -> 0}]
Out[8]=
In[9]:=
URLExecute[api, {"x" -> 0, "y" -> 0}]
Out[9]=

Resource History