How to encode/decode values into a string sent by sendActionMessage()

I have a component I want to send data values to another component. So I made the one component an Action Broadcaster, and the other an ActionListener. It all works, I can send a String from one to the other.

Where I’m stuck at the moment, is how do I encode say two or more integers into the string, and then decode them on the other end? Could not seem to locate an example so far. Thanks!

You could use JSON::toString / JSON::fromString and construct the messages and vars to then send as JSON – but I’d recommend creating either a more global / abstract messaging system for system wide events, or using the how the slider uses listener lists and nested virtual listener classes for more explicit communication layers.

If you just want to send / receive string then the action listener will work, but you may find yourself doing strange stuff to get the data you need to pass through it.

If I do use that I tend do something like:

THINGA -> Sending Something Happened

THINGB-> Receives Something Happened -> Checks with Thing A

1 Like

Look at the JUCE ListenerList class: Also look how it’s used internally in JUCE. Try and copy how they use it.

I try and avoid ActionBroadcaster / ActionListener as much as possible as I find it creates fragile code. If you change the format of the encoded string, it’s easy to miss a listener and forget to update the format. Also if one listener is listening to multiple broadcasters, it can be hard to tell what messages are coming from what broadcaster. Very easy to break things when refactoring.

ListenerLists however, all the parameters are strongly typed, so it’s obvious when something changes.

Also, if there is only one sender and one receiver, look into using std::function for your callbacks instead of a broadcaster / listener.


Thanks to both of you! I have reimplemented this with the ListenerList class, and it works great, I can pass a function with however many typed arguments I want.

I actually used the ComboBox as an example, since it seemed the Slider has an extra layer of complexity built into it with this whole Pimpl thing…

1 Like