require("estacion")
const { EventBus } = require('estacion')
// or import { EventBus } from 'estacion'
// create event bus
const bus = new EventBus()
// create channels
const usersChannel = bus.channel('users')
const gamesChannel = bus.channel('games')
// create topics for the channel (optional)
const userAdded = usersChannel.topic('user_added')
const userRemoved = usersChannel.topic('user_removed')
// create topics for the channel (optional)
const gameStart = gamesChannel.topic('game_start')
const gameEnd = gamesChannel.topic('game_end')
// create listener
const listener = event => {
console.log(event.channel)
console.log(event.topic)
console.log(event.payload) // custom payload from the event
}
// add listener to the channel
usersChannel.addListener(listener)
// or add listener to the channel topic
userAdded.addListener(listener)
// emit event on the channel
const customPayload = {} // whatever you like
usersChannel.emit(customPayload)
// advanced
// subscribe to all channels and topics (more in the docs)
bus.mainChannel().addListener(listener)
// advanced
// subscribe to all channels but only for a particular topic
// now you will listen to 'game_end' on any channel
bus
.mainChannel()
.topic('game_end')
.addListener(listener)
// or emit event on the topic
userAdded.emit({ name: 'Sam', lastName: 'Fisher' })