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]:=
listener = ChannelListen["example"]
Out[1]=
In[2]:=
(ChannelSend["example", #]) & /@ {"string", 100, True, {1, 2, 3},
"a" -> "b", {"c" -> 1}, <|"d" -> True|>};
In[3]:=
listener["Message", All]
Out[3]=
Messages containing code are safely wrapped in HoldComplete, giving you an opportunity to examine them before evaluating.
In[4]:=
ChannelSend["example", Unevaluated[Print["hello"]]];
In[5]:=
listener["Message"]
Out[5]=
In[6]:=
listener["Message"] // ReleaseHold
Create a channel that sends a compressed notebook and opens the received notebook in a new window.
In[7]:=
CreateChannel["nb", ChannelPreSendFunction -> (Compress[#1] &),
HandlerFunctions ->
Association[
"MessageReceived" -> (CreateWindow[Uncompress[#Message]] &)]]
Out[7]=
Start listening on the channel.
In[8]:=
listener = ChannelListen["nb"]
Out[8]=
Create a notebook and send it to the channel.
In[9]:=
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]:=
ChannelSend["nb", nb]
Out[10]=
The received notebook.
Out[11]=