Wolfram Language

Channel Framework

Process Any Type of Data

Messages that can be represented in the JSON format (strings, numbers, True, False, or Null), or lists, rules, or associations containing such elements, are given literally.

In[1]:=
Click for copyable input
listener = ChannelListen["example"]
Out[1]=
In[2]:=
Click for copyable input
(ChannelSend["example", #]) & /@ {"string", 100, True, {1, 2, 3}, "a" -> "b", {"c" -> 1}, <|"d" -> True|>};
In[3]:=
Click for copyable input
listener["Message", All]
Out[3]=

Messages containing code are safely wrapped in HoldComplete, giving you an opportunity to examine them before evaluating.

In[4]:=
Click for copyable input
ChannelSend["example", Unevaluated[Print["hello"]]];
In[5]:=
Click for copyable input
listener["Message"]
Out[5]=
In[6]:=
Click for copyable input
listener["Message"] // ReleaseHold

Create a channel that sends a compressed notebook and opens the received notebook in a new window.

In[7]:=
Click for copyable input
CreateChannel["nb", ChannelPreSendFunction -> (Compress[#1] &), HandlerFunctions -> Association[ "MessageReceived" -> (CreateWindow[Uncompress[#Message]] &)]]
Out[7]=

Start listening on the channel.

In[8]:=
Click for copyable input
listener = ChannelListen["nb"]
Out[8]=

Create a notebook and send it to the channel.

In[9]:=
Click for copyable input
nb = DocumentNotebook[{TextCell["My Report", "Section"], TextCell["Lorem ipsum dolor sit amet:", "Text"], ExpressionCell[ ContourPlot[x + Sin[x^2 + y^2], {x, -4, 4}, {y, -4, 4}, Contours -> 9, ContourShading -> ColorData[35, "ColorList"]]]}, StyleDefinitions -> FrontEnd`FileName[{"Article"}, "Preprint.nb"], WindowTitle -> "My Report", Background -> LightOrange]
Out[9]=
In[10]:=
Click for copyable input
ChannelSend["nb", nb]
Out[10]=

The received notebook.

Out[11]=

Related Examples

de es fr ja ko pt-br ru zh