({{, (RefLink[ChannelPreSendFunction, paclet:ref/ChannelPreSendFunction]) 
is an option to (RefLink[ChannelObject, paclet:ref/ChannelObject]) and related functions that specifies a function to apply to outgoing messages before they are sent to the server.}})

OpenersNotesSection(Spacer1)Details

With (RefLink[ChannelPreSendFunction, paclet:ref/ChannelPreSendFunction])->f, the function f receives the message msg supplied in (RefLink[ChannelSend, paclet:ref/ChannelSend])[channel,msg] and options in the form of an association or a list of rules.

Specifying the (RefLink[ChannelPreSendFunction, paclet:ref/ChannelPreSendFunction]) option in (RefLink[ChannelSend, paclet:ref/ChannelSend])[channel,msg,ChannelPreSendFunction->f] lets you send a message on the channel without enabling (RefLink[$AllowExternalChannelFunctions, paclet:ref/$AllowExternalChannelFunctions]) globally.

OpenersPrimaryExamplesSection(Spacer1)Examples  (2)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create a channel with a custom preprocessing function:

channel=CreateChannel["comp",ChannelPreSendFunction->(Compress[#]&)]

Start listening on the channel and send a message to it:

listener=ChannelListen[channel]

ChannelSend[channel,"hello"]

The received message:

listener["Message"]

Restore the original message content:

Uncompress[%]

Remove the listener and delete the channel:

RemoveChannelListener[listener]

DeleteChannel[channel]

Sending encrypted messages:

channel=CreateChannel["enc",ChannelPreSendFunction->(Encrypt["secret",#]&)]

Set up a listener and send a message:

listener=ChannelListen[channel]

ChannelSend[channel,"hello again"]

The message is encrypted:

listener["Message"]

Retrieve the message:

Decrypt["secret",ReleaseHold[%]]

Cleanup:

RemoveChannelListener[listener]

DeleteChannel[channel]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ChannelSend, paclet:ref/ChannelSend])  ▪  (RefLink[CreateChannel, paclet:ref/CreateChannel])  ▪  (RefLink[ChannelObject, paclet:ref/ChannelObject])  ▪  (RefLink[ChannelListen, paclet:ref/ChannelListen])  ▪  (RefLink[$Pre, paclet:ref/$Pre])}})}})

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