({{, (RefLink[ChannelReceiverFunction, paclet:ref/ChannelReceiverFunction])[fun] 
represents a channel receiver function that applies fun to any channel message it receives.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[CloudDeploy, paclet:ref/CloudDeploy])[ChannelReceiverFunction[…]] yields a (RefLink[CloudObject, paclet:ref/CloudObject]) containing meta-information that specifies the channel to which messages for the channel receiver should be sent.

(RefLink[CloudDeploy, paclet:ref/CloudDeploy])[ChannelReceiverFunction[…]->"curi"] deploys the receiver to (RefLink[ChannelObject, paclet:ref/ChannelObject])["curi"].

(RefLink[CloudDeploy, paclet:ref/CloudDeploy])[ChannelReceiverFunction[…]->channel] deploys the receiver to the specified channel, given as (RefLink[ChannelObject, paclet:ref/ChannelObject])[…].

(RefLink[CloudDeploy, paclet:ref/CloudDeploy])[ChannelReceiverFunction[…]] is equivalent to (RefLink[CloudDeploy, paclet:ref/CloudDeploy])[ChannelReceiverFunction[…]->(RefLink[Automatic, paclet:ref/Automatic])].

In a deployed (RefLink[ChannelReceiverFunction, paclet:ref/ChannelReceiverFunction])[fun], fun[assoc] is evaluated whenever a channel message is received. The association assoc contains the same elements as in (RefLink[ChannelListen, paclet:ref/ChannelListen]), except for ones specific to the (RefLink[ChannelListener, paclet:ref/ChannelListener]) object.

(RefLink[CloudDeploy, paclet:ref/CloudDeploy])[ChannelReceiverFunction[fun]] sets up a channel receiver function that is executed in the cloud and does not require active listeners in any specific Wolfram Language session.

The function fun is evaluated in the environment of the owner of the channel receiver function.

OpenersPrimaryExamplesSection(Spacer1)Examples  (13)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Define a receiver function:

f=ChannelReceiverFunction[{#Message,#MessageID}&]

Apply the function to an association representing a channel broker message:

f[<|"Message"->"message","MessageID"->"id"|>]

Define a receiver function that logs the incoming message to a cloud object:

f=ChannelReceiverFunction[CloudPut[#Message,CloudObject[ "LastMessage"]]&]

Deploy the function:

object=CloudDeploy[f]

Obtain the target channel of the receiver:

channel=ChannelObject[object]

Send a message to the channel:

ChannelSend[channel,"hello"]

Check the cloud object:

CloudGet[CloudObject["LastMessage"]]

OpenersExampleSection1(Spacer1)Scope  (7)

Apply to an association:

ChannelReceiverFunction[#&][<|"Message"-><|"a"->"b"|>,"MessageID"->"id"|>]

Apply to a string representing a JSON object:

ChannelReceiverFunction[#Message&]["{\"x\":\"1\"}"]

Deploy to an anonymous cloud object and anonymous channel object:

CloudDeploy[ChannelReceiverFunction[#&]]

ChannelObject[%]

Deploy to the specified channel object:

CloudDeploy[ChannelReceiverFunction[#&]->"channel"]

ChannelObject[%]

Deploy to the specified cloud object:

CloudDeploy[ChannelReceiverFunction[#&],"cloudObject"]

Deploy to the specified channel object and the specified cloud object:

CloudDeploy[ChannelReceiverFunction[#&]->"channel","cloudObject"]

ChannelObject[%]

Specify the targets as objects:

CloudDeploy[ChannelReceiverFunction[#&]->ChannelObject["channel"],CloudObject["x"]]

ChannelObject[%]

OpenersExampleSection3(Spacer1)Applications  (3)

Deploy a channel that adds incoming messages to a databin:

bin = CreateDatabin[]

With[{bin=bin},CloudDeploy[ChannelReceiverFunction[DatabinAdd[bin,<|"Data"->#Message|>]&]->"channel"]]

Send a message to the channel and retrieve its content from the databin:

ChannelSend["channel","hello"]

bin["Entries"]

Define a receiver function that sends messages containing the string "URGENT" to a priority channel and the rest to a bulk channel:

f=ChannelReceiverFunction[ChannelSend[If[StringContainsQ[#Message,"URGENT"],"priority","bulk"],#Message]&]

Deploy the receiver function:

CloudDeploy[f->"dispatcher"]

Start listening on the channels:

priority=ChannelListen["priority"]

bulk=ChannelListen["bulk"]

Send a regular and an urgent message on the dispatcher channel:

ChannelSend["dispatcher",#]&/@{"URGENT message","low priority"}

The received messages:

priority["Message"]

bulk["Message"]

Deploy a receiver function that sends an email alert if a message arrives when nobody is listening on the channel:

channel="withAlert";

CloudDeploy[ChannelReceiverFunction[If[ChannelSubscribers[channel]==={},SendMail[<|"To"->$WolframID,"Subject"-> "missed message from "<>#RequesterWolframID, "Body" -> #Message|>]]&]->channel]

Send a message on the channel and check your mailbox for an email alert:

ChannelSend[channel,"Are you there?"]

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

Deploying (RefLink[ChannelReceiverFunction, paclet:ref/ChannelReceiverFunction]) sets (RefLink[ChannelBrokerAction, paclet:ref/ChannelBrokerAction]) for the associated channel object:

CloudDeploy[ChannelReceiverFunction[#&]]

Options[ChannelObject[%],ChannelBrokerAction]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ChannelListen, paclet:ref/ChannelListen])  ▪  (RefLink[MailReceiverFunction, paclet:ref/MailReceiverFunction])  ▪  (RefLink[APIFunction, paclet:ref/APIFunction])  ▪  (RefLink[ChannelDatabin, paclet:ref/ChannelDatabin])  ▪  (RefLink[ChannelBrokerAction, paclet:ref/ChannelBrokerAction])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Channel-Based Communication, paclet:guide/Channel-BasedCommunication])}, {▪(RefLinkPlain[Cloud Functions & Deployment, paclet:guide/CloudFunctionsAndDeployment])}})}})