There are tutorials that show how that is done via Particle.function() or Particle.subscribe() (previously Spark.xxxx()) in the Tutorials section.
Or even in the docs
https://docs.particle.io/reference/javascript/
https://docs.particle.io/reference/javascript/#callfunction
https://docs.particle.io/reference/javascript/#publishing-event