MQTT: How to publish to all clients? #3032
-
Hi! I successfully subscribe to a topic via template as cotonic.broker.subscribe("my/topic", () => {
console.log("hello")
}) In the server {ok, ClientId} = z_context:client_id(Context),
z_mqtt:publish([<<"bridge">>, ClientId, <<"my">>, <<"topic">>], #{}, Context).
%% or
z_mqtt:publish([<<"~client">>, <<"my">>, <<"topic">>], #{}, Context). ... the current client receives the information on the page, but only the client who fired the event/topic. How to propagate the event to all clients on the current page? How to, via server, all clientes/pages subscribed in the topic Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Hello, Complementing the question, we are removing the pubzub library and the same problem occurs when we tried this via .js doing Our scenario is: a modification generated by one user on a page/URL has to force a refresh to all other users accessing the same page/URL. All users are authenticated. Any help is appreciated. |
Beta Was this translation helpful? Give feedback.
-
If you to publish to all clients, then all those clients should be subscribed to a topic on the server. cotonic.broker.subscribe("bridge/origin/my/topic", () => {
console.log("hello")
}) And then on the server publish to Note that all subscribes and publishes are ACL checked, so make certain that your topic can be subscribed to by the client. For testing you can use the cotonic.broker.subscribe("bridge/origin/test/foo/bar", () => {
console.log("hello")
}) |
Beta Was this translation helpful? Give feedback.
-
I changed the For the model topics, you need to have From % Model events can be view iff the user has use permission on the module
is_allowed(subscribe, [ <<"model">>, Model, <<"event">> | _ ], Context) ->
try
case z_module_indexer:find(model, Model, Context) of
{ok, #module_index{ module = zotonic_core }} ->
true;
{ok, #module_index{ module = Module }} ->
z_acl:is_allowed(use, Module, Context);
{error, enoent} ->
false
end
catch
_:_ ->
false
end; |
Beta Was this translation helpful? Give feedback.
-
Hello Marc, That was a question we had yesterday. This model topic refers to the Zotonic model, Cotonic model or is it a new concept? Cheers, |
Beta Was this translation helpful? Give feedback.
-
The model is a Zotonic concept that is also used by Cotonic. In your case, as the topic is not associated with a particular model, I would make a special topic. It is good to make a list of topics that should be interesting or should have some default access control attached to them. We have already the |
Beta Was this translation helpful? Give feedback.
If you to publish to all clients, then all those clients should be subscribed to a topic on the server.
And then on the server publish to
my/topic
Note that all subscribes and publishes are ACL checked, so make certain that your topic can be subscribed to by the client.
For testing you can use the
test
orpublic
topics, those are publicly available.