Options
All
  • Public
  • Public/Protected
  • All
Menu

Module eventbus

The eventbus is a global pub/sub system. It is useful for your own code, and it also the mechanism that allows your code to communicate with code that others are running on the server.

Use it to publish to a topic, and other players can consume the data by subscribing to the same topic.

Index

Type aliases

MessageData

MessageData: object

Type declaration

  • data: string | Object
  • type: string

SubscriptionCallback

SubscriptionCallback: function

Your subscription callback function will receive a single parameter of type MessageData.

Type declaration

Functions

cancelAllSubscriptions

  • cancelAllSubscriptions(): boolean
  • Sometimes, things get real messed up and you just need to hit the reset button.

    You can quit from the server and rejoin, and this will clear all your eventbus subscriptions.

    You can also call eventbus.cancelAllSubscriptions(). This will cancel all subscriptions you have created for all topics.

    Returns boolean

publish

  • publish(topic: string, data: string | Object): any
  • Publish to a topic. You can publish a string, or a JSON Object.

    eventbus.publish('sitapati.messages', 'Hope everyone is feeling fabulous today!');
    
    eventbus.publish('sitapati.messages', {data: 'You can also publish JSON objects', moredata: 'The consumer should check the data type in their subscription'});
    

    Parameters

    • topic: string
    • data: string | Object

    Returns any

subscribe

  • Subscribe to a topic on the eventbus. Topic names are arbitrary, and the topic namespace is global, so you probably want to namespace your topics, for example by using your username, like: 'sitapati.myTopic'

    const sub = eventbus.subscribe('sitapati.messages', (msg) => {
         if (msg.type === "string") {
             magik.dixit(msg.data);
         }
         if (msg.type === "json") {
             magik.dixit(Object.keys(msg.data).toString());
         }
    });
    

    Parameters

    Returns Subscription

unsubscribeFromTopic

  • unsubscribeFromTopic(topic: string): boolean
  • To cancel a subscription, you call the cancel() method of the subscription object returned from eventbus.subscribe(). However, sometimes you lose the reference to that object - or you want to unsubscribe a bunch of listeners all at once.

    In this case, you can use eventbus.unsubscribeFromTopic(). You specify which topic you want to unsubscribe from, and all your subscriptions to that topic will be cancelled.

    This does not unsubscribe anyone else - just your subscriptions.

    // Subscribe without getting a reference to the subscription
    eventbus.subscribe('magikcraft.news', news => magik.dixit(news.data));
    ...
    // Somewhere else, we don't have a subscription to cancel, but we can cancel all subscriptions to that topic:
    eventbus.unsubscribeFromTopic('magikcraft.news');
    

    Parameters

    • topic: string

    Returns boolean

Generated using TypeDoc