ChannelListen

ChannelListen[channel]

starts listening on the specified channel.

ChannelListen[channel,func]

applies func to the association corresponding to each message received on the channel.

ChannelListen[channel,None]

stores each message received on the channel, without applying any function.

ChannelListen[url]

listens on the specified URL, storing messages received, without requiring an explicit channel to exist on the channel broker.

Details and Options

  • ChannelListen[] gives a ChannelListener object.
  • In ChannelListen[channel,], channel can be specified as a channel object or in any of the forms used inside ChannelObject.
  • ChannelListen[channel] is equivalent to ChannelListen[channel,Automatic].
  • If specified in ChannelListen[channel,func], func overrides the "MessageReceived" handler of channel. It is called with the association containing the following elements:
  • "Message"contents of the message
    "MessageID"unique message ID
    "Timestamp"server timestamp for the message
    "RequesterWolframID"Wolfram ID of the sender
    "RequesterWolframUUID"Wolfram UUID of the sender
    "ListenerID"ID of the current listener
  • The contents of a message are given literally if the message is a string, Real, Integer, ByteArray, True, False, Null, Image or Image3D, or a list or association of such objects. All other messages are given either in the form HoldComplete[DisableFormatting[expr]] or as a list or association of delayed rules of the form "name"DisableFormatting[val].
  • For security reasons, you should exercise caution in accessing unevaluated message contents, such as parts of held expressions, right-hand sides of delayed rules, or applying ToExpression to string messages.
  • ChannelListen accepts the following options:
  • AutoRemoveFalsewhether to remove the listener after receiving one message
    ChannelBase$ChannelBasedefault channel base to assume
    ChannelHistoryLength100how many messages to store in the listener cache
    NotificationFunctionInheritednotification to give when a message arrives
  • A channel listener set up by ChannelListen can be removed using RemoveChannelListener.
  • Removing a channel listener discards messages cached by the listener.
  • Terminating a Wolfram Language session removes all active channel listeners.

Examples

open allclose all

Basic Examples  (2)

Create a channel and start listening:

Send a message to the channel and retrieve the contents of the message from the listener:

Remove the listener and delete the channel:

Define a custom handler function that will be executed when a message arrives:

Sending a message to the channel causes the function to be called:

The message is also recorded in the ChannelListener object:

Remove the listener and delete the channel:

Scope  (4)

Channel Specifications  (2)

Listen on a channel defined by a ChannelObject:

Listen on a channel in your home area on the channel broker server, specifying its name:

Listen on a channel specified by its fully qualified name:

Listen on a channel specified by its full URL:

Delete the channel, which removes the listener:

Listen on a channel in the common area on the server, specified by a URL:

Delete the channel, which removes the listener:

Handlers  (2)

Examine the arguments received by the handler function:

Sending a message to the channel prints the association received by the handler:

Delete the channel, which removes the listener:

Do not execute any handler function when messages are received:

You can still retrieve messages sent to the channel:

Delete the channel, which removes the listener:

Options  (3)

AutoRemove  (1)

Launch a listener that will remove itself after the first message is received:

The listener is active:

The listener is automatically removed after a message is sent and processed:

Delete the channel:

ChannelHistoryLength  (1)

Create a channel listener with ChannelHistoryLength set to 10:

Send 20 messages to the channel:

Only the 10 most-recently received messages are stored:

Delete the channel, which removes the listener:

NotificationFunction  (1)

Define a custom notification function in the listener:

The function is executed once a message is sent and processed:

Delete the channel, which removes the listener:

Applications  (4)

Email  (1)

In a desktop session, set up a listener that speaks the subject of an incoming message:

Create a MailReceiverFunction that forwards received mail to your listener:

Send an email to the receiver function and hear your listener speak:

Delete the channel, which removes the listener:

Remote Evaluation  (1)

In your Wolfram Cloud account, or any other remote Wolfram System, set up a listener that evaluates incoming messages and sends the result back to a specified channel:

Set up a receiving channel on your local system:

Send a command to the remote evaluator:

Access the response received:

Verify that the result is coming from a different machine:

Clean up:

Wolfram Cloud  (1)

ChannelListen allows your current session to receive updates from APIFunction, FormFunction and other functions as they are executed in the Wolfram Cloud:

Deploy a webpage that performs a simple operation and sends the result along with some metadata to the listener:

The listener dynamically updates as soon as the web form is submitted:

Delete the channel:

Social Media  (1)

Create an APIFunction that forwards the contents of HTTP requests to a channel:

Create an IFTTT applet that tracks mentions of the word "Mathematica" on Twitter and sends them to the APIFunction via HTTP:

Create a listener on the channel:

Create a service connection to Twitter and send a tweet containing the keyword:

List the messages received by the listener:

Delete the channel:

Properties & Relations  (3)

ChannelListen returns a ChannelListener object:

You can query the listener and retrieve its properties:

Delete the channel, which removes the listener:

Use RemoveChannelListener to remove a ChannelListener object:

Delete the channel:

On desktop Wolfram systems, output of Print executed in the message handler function is directed to the Messages window:

Delete the channel, which removes the listener:

Possible Issues  (1)

For security reasons, you cannot listen on other users' channels by default:

If you trust the channel author to potentially execute a message handler function on your computer, you can temporarily override the security setting:

Restore the default:

Neat Examples  (1)

Start listening on a channel and arrange to dynamically plot incoming points as they arrive:

Send data to the channel from the current session or from any other Wolfram Language session connected to the internet:

Set up a task to send points periodically to a channel's URL:

Stop sending data and delete the channel:

Introduced in 2016
 (11.0)
 |
Updated in 2020
 (12.1)