Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/molda/pubsub
Simple Publish/Subscribe module
https://github.com/molda/pubsub
module pubsub totaljs
Last synced: about 1 month ago
JSON representation
Simple Publish/Subscribe module
- Host: GitHub
- URL: https://github.com/molda/pubsub
- Owner: molda
- License: mit
- Created: 2019-05-28T18:08:57.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-10-12T08:37:54.000Z (over 2 years ago)
- Last Synced: 2024-12-16T02:42:27.785Z (about 1 month ago)
- Topics: module, pubsub, totaljs
- Language: JavaScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PubSub
Simple implementation of Publisher/Subscriber system as a module for Total.js framework
Client library is served at /@pubsub/client.js
It reconnects automaticaly when a connection is lost.## Instalation
Just place the pubsub.js file in /modules/ folder.## Usage
Server:
```javascript
// subscribe to a single topic
PUBSUB.subscribe('hello');// subscribe to a multiple topics
PUBSUB.subscribe([ 'hello2', 'hello3' ]);// unsubscribe from a single topic
PUBSUB.unsubscribe('hello');// unsubscribe from a multiple topics
PUBSUB.unsubscribe([ 'hello2', 'hello3' ]);// listen for a message with 'hello' topic
PUBSUB.on('hello', function(msg){
console.log('hello', msg);
});// listen for all the message subscribed to
PUBSUB.on('message', function(topic, msg){
console.log('message', topic, msg);
});// publish a message with 'hello' topic
PUBSUB.publish('hello', 'world');
```Client:
```javascript
var pubsub = new PubSub();// event emited once the websocket successfuly connects to the server
pubsub.on('connected', function(){
console.log('connected');// subscribe to a single topic
pubsub.subscribe('hello');// subscribe to a multiple topics
pubsub.subscribe([ 'hello2', 'hello3' ]);// unsubscribe from a single topic
pubsub.unsubscribe('hello');// unsubscribe from a multiple topics
pubsub.unsubscribe([ 'hello2', 'hello3' ]);
});// event emited when the websocket disconnects from the server
pubsub.on('disconnected', function(){
console.log('disconnected');
});// listen for a message with 'hello' topic
pubsub.on('hello', function(msg){
console.log('hello', msg);
});// listen for all the message subscribed to
pubsub.on('message', function(topic, msg){
console.log('message', topic, msg);
});
```