Phoenix PubSub include name of topic in dispatched message?

Hi all,

I have a process that subscribes to multiple topics on a pubsub and I want the pubsub to send messages like {topic, message} instead of just message so I can tell what topic I’m receiving information from. How can I accomplish this without slowing anything down? A custom dispatcher? An option I missed somewhere?

I don’t know what your message look like, but I like to use self contained message, with some metadata.

In your case, that could be …

%{topic: topic, payload: message}

I usually don’t do this, but I pass type: event_type, and maybe some id.

1 Like

I guess including it in the message is the fastest possibliity.

Seems like the topic is basically designed not to propagate from the pubsub

The dispatcher must be available on all nodes running the PubSub system. The dispatch/3 function of the given module will be invoked with the subscriptions entries, the broadcaster identifier (either a pid or :none), and the message to broadcast.
– pubsub documentation

so topic isn’t even given to the dispatcher, it’s really gone. i have to include this information in the message it seems.

alright your structure it is.