logo small

PHP Publish Subscribe: Register and call handlers of events by name

Recommend this page to a friend!
Download Download
Info View files Files Install with Composer Install with Composer Download Download Reputation Support forum Blog Links
Last Updated Ratings Unique User Downloads Download Rankings
2025年10月10日 (Less than 1 hour ago) RSS 2.0 feed Not yet rated by the users Total: 45 All time: 10,816 This week: 660Up
Version License PHP version Categories
publishsubscribe 2.0.0 Artistic License 5 PHP 5 , Language
Description

Author

This package can register and call handlers of events by name.

It can register callback functions that will be called when an event with a given name happens.

The package can also process events that are triggered and call all functions that were registered to handled the called event.

Besides the PHP version it also provides implementations of this publish and subscribe solution in JavaScript and Python.

Picture of Nikos M.
Name: Nikos M. is available for providing paid consulting. Contact Nikos M. .
Classes: 21 packages by
Country: Greece Greece
Age: 49
All time rank: 8449 in Greece Greece
Week rank: 195 Up2 in Greece Greece Up
Innovation award
Innovation award
Nominee: 8x

Winner: 2x

Details

PublishSubscribe

A simple and flexible publish-subscribe pattern implementation for PHP, JavaScript, Python

PublishSubscribe

version: 2.0.0

Supports nested topics, tagged topics and namespaced topics.

Topic structure:

[Topic1[/SubTopic11/SubTopic111 ...]][#Tag1[#Tag2 ...]][@NAMESPACE1[@NAMESPACE2 ...]]
  • A topic can be nested with one or more levels, all matching levels will be notified in order from specific to general
  • A topic can also be tagged with one or more tags, only matching levels whose registered tags are matched will be notified
  • A topic can also be namespaced with one or more namespaces, all matching levels will be notified if no namespace given when event triggered, else only the levels whose namespaces are matched (this is similar to tags above)
  • All/Any of the above can be used simultaneously, at least one topic OR tag OR namespace should be given for an event to be triggered

Namespaces work similarly to jQuery namespaces (so handlers can be unsubscribed based on namespaces etc..).

The difference, between tags and namespaces, is that when a topic is triggered without tags and namespaces, handlers which match the topic will be called regardless if they have namespaces or not, while handlers that match the topic but also have tags will not be called.

All topic separators (i.e "/", "#", "@") are configurable per instance.

During the publishing process, an event can be stopped and/or cancel the bubble propagation.

API

see /test folder

const pb = new PublishSubscribe();
// set topic/tag/namespace separators for this pb instance
// defaults are:
// Topic separator = "/"
// Tag separator = "#"
// Namespace separator = "@"
pb.option('topic_separator', "/");
pb.option('tag_separator', "#");
pb.option('namespace_separator', "@");
// subscribe a handler for a topic with optional tags and optional namespaces
pb.on(topic_with_tags_namespaces, handler);
// subscribe a handler only once for a topic with optional tags and optional namespaces
// handler automatically is unsubscribed after it is called once
pb.one(topic_with_tags_namespaces, handler);
// subscribe a handler on top (first) for a topic with optional tags and optional namespaces
pb.on1(topic_with_tags_namespaces, handler);
// subscribe a handler only once on top (first) for a topic with optional tags and optional namespaces
// handler automatically is unsubscribed after it is called once
pb.one1(topic_with_tags_namespaces, handler);
// unsubscribe a specific handler or all handlers matching topic with optional tags and optional namespaces
pb.off(topic_with_tags_namespaces [, handler = null]);
// publish a topic with optional tags and optional namespaces and pass any data as well
pb.emit(topic_with_tags_namespaces, data);
// pipeline allows to call subscribers of given topic/message asynchronously via a pipeline
// each subscriber calls next subscriber via the passed event's .next() method
// pipeline can be aborted via the passed event's .abort() method
// optional finish_callback will be called when the pipeline finishes the chain or event is aborted
pb.pipeline(topic_with_tags_namespaces, data [, abort_callback [, finish_callback]]);
// dispose PublishSubscribe instance
pb.disposePubSub();

see also:

  • ModelView a simple, fast, powerful and flexible MVVM framework for JavaScript
  • tico a mini, super-simple MVC framework for PHP
  • LoginManager a simple, barebones agnostic login manager for PHP, JavaScript, Python
  • SimpleCaptcha a simple, image-based, mathematical captcha with increasing levels of difficulty for PHP, JavaScript, Python
  • Dromeo a flexible, and powerful agnostic router for PHP, JavaScript, Python
  • PublishSubscribe a flexible publish-subscribe pattern implementation for PHP, JavaScript, Python
  • Localizer a simple and versatile localization class (l10n) for PHP, JavaScript, Python
  • Importer simple class & dependency manager and loader for PHP, JavaScript, Python
  • EazyHttp, easy, simple and fast HTTP requests for PHP, JavaScript, Python
  • Contemplate a fast and versatile isomorphic template engine for PHP, JavaScript, Python
  • HtmlWidget html widgets, made as simple as possible, both client and server, both desktop and mobile, can be used as (template) plugins and/or standalone for PHP, JavaScript, Python (can be used as plugins for Contemplate)
  • Paginator simple and flexible pagination controls generator for PHP, JavaScript, Python
  • Formal a simple and versatile (Form) Data validation framework based on Rules for PHP, JavaScript, Python
  • Dialect a cross-vendor & cross-platform SQL Query Builder, based on GrammarTemplate, for PHP, JavaScript, Python
  • DialectORM an Object-Relational-Mapper (ORM) and Object-Document-Mapper (ODM), based on Dialect, for PHP, JavaScript, Python
  • Unicache a simple and flexible agnostic caching framework, supporting various platforms, for PHP, JavaScript, Python
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python

Files folder image Files (17)
File Role Description
Files folder imagesrc (3 directories)
Files folder imagetest (3 directories)
Accessible without login Image file publishsubscribe.jpg Icon Icon image
Accessible without login Plain text file README.md Doc. Documentation

Files folder image Files (17) / src
File Role Description
Files folder imagejs (2 files)
Files folder imagephp (1 file)
Files folder imagepy (2 files)

Files folder image Files (17) / src / js
File Role Description
Accessible without login Plain text file PublishSubscribe.js Data Auxiliary data
Accessible without login Plain text file PublishSubscribe.min.js Data Auxiliary data

Files folder image Files (17) / src / php
File Role Description
Plain text file PublishSubscribe.php Class Class source

Files folder image Files (17) / src / py
File Role Description
Accessible without login Plain text file PublishSubscribe.py Data Auxiliary data
Accessible without login Plain text file __init__.py Data Auxiliary data

Files folder image Files (17) / test
File Role Description
Files folder imagejs (4 files)
Files folder imagephp (4 files)
Files folder imagepy (2 files)

Files folder image Files (17) / test / js
File Role Description
Accessible without login Plain text file out.txt Doc. Documentation
Accessible without login Plain text file test.js Data Auxiliary data
Accessible without login Plain text file test_filter.js Data Auxiliary data
Accessible without login Plain text file test_pipeline.js Data Auxiliary data

Files folder image Files (17) / test / php
File Role Description
Accessible without login Plain text file out.txt Doc. Documentation
Accessible without login Plain text file test.php Example Example script
Accessible without login Plain text file test_filter.php Example Example script
Accessible without login Plain text file test_pipeline.php Example Example script

Files folder image Files (17) / test / py
File Role Description
Accessible without login Plain text file out.txt Doc. Documentation
Accessible without login Plain text file test.py Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Version Control Unique User Downloads Download Rankings
100%
Total: 45
This week: 0
All time: 10,816
This week: 660 Up
About us About us Advertise on this site Advertise on this site Site map Site map Newsletter Newsletter Statistics Statistics Site tips Site tips Privacy policy Privacy policy Contact Contact

AltStyle によって変換されたページ (->オリジナル) /