A simple PubSub
- subscribe a function to a channel:
let subscriptionId = kiss.pubsub.subscribe("MY_CHANNEL_NAME", (messageData) => { console.log(messageData) })
- publish a message on a channel:
kiss.pubsub.publish("MY_CHANNEL_NAME", {foo: "bar"})
- unsubscribe a function:
kiss.pubsub.unsubscribe(subscriptionId)
- list all active subscriptions
kiss.pubsub.list()Methods
# static get(subscriptionId) → {function}
Get a subscription by id.
Mainly used for debug purpose, when you need to check which function is registered in the pubsub.
Parameters:
| Name | Type | Description |
|---|---|---|
subscriptionId |
string
|
The id of the subscription to retrieve |
The subscribed function
function
# static getCount() → {number}
Return the number of subscriptions.
Mainly used for debugging (useful to track memory leaks).
number
# static list(showFunctionopt)
List all the subscriptions in the console.
Mainly used for debug purpose, when you need an overview of subscriptions.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
showFunction |
boolean
|
<optional> |
If true, show the subscribed function |
# static publish(channel, messageDataopt)
Publish a message on a specific channel + on the global channel, if any
The global channel is the "*" channel, which is a special channel that will be called for all messages. Sometimes, it can be convenient to observe all messages, without subscribing to a specific channel.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
channel |
string
|
The channel name |
|
messageData |
object
|
<optional> |
The data published into the channel |
# static publishOnGlobalChannel(messageData)
Publish a message on the global channel (*)
Parameters:
| Name | Type | Description |
|---|---|---|
messageData |
*
|
# static subscribe(channel, fn, descriptionopt) → {string}
Subscribe a function to a channel
The global channel is the "*" channel, which is a special channel that will be called for all messages. Sometimes, it can be convenient to observe all messages, without subscribing to a specific channel.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
channel |
string
|
The channel name, or "*" to subscribe to all channels |
|
fn |
function
|
The function to subscribe to the channel |
|
description |
string
|
<optional> |
Optional description of the subscription |
The subscription id
string
# static subscribeOnce(channel, fn, descriptionopt) → {string}
Subscribe a function to a channel, but unsubscribe it after the first call
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
channel |
string
|
The channel name |
|
fn |
function
|
The function to subscribe to the channel |
|
description |
string
|
<optional> |
Optional description of the subscription |
The subscription id
string
# static unsubscribe(subscriptionId)
Unsubscribe a function from the pubsub
Parameters:
| Name | Type | Description |
|---|---|---|
subscriptionId |
string
|
The id of the subscription to remove |