@includable/topics
v1.4.0
Published
Client library for Includable Topics.
Downloads
3
Readme
Topics
Super simple websocket message broadcasting.
Topis is a service built into the Includable Platform, making it easy for Includable app developers to use websocket functionality.
Note however that when using this library, the namespace shared by your topics is shared by all users of the library. So make sure to make your topic name something unique that won't collide with other users.
The best approach for this is to use a reverse-DNS style string, like
com.thomasschoffelen.mytopic
.
Installation
npm i @includable/topics
Usage
See example.js
.
API
The Topics class exposes the following (static) functions. All of these
functions return a Promise
object.
Topics.sub
Subscribe to a topic.
Topics.sub('my-topic-name', (message) => {
console.log('Received message on my-topic-name: ' + message)
})
Note that topics can be subscribed to only once, but you can use this method multiple times to attach multiple callbacks to the same topic.
Topics.unsub
Unsubscribe from a topic.
Topics.unsub('my-topic-name')
Topics.broadcast
Broadcast a message to a topic. These messages should always be plain (stringify-able) JS objects.
Topics.broadcast('my-topic-name', {'hello': 'world'})
Topics.start
Connect to the Topics server. It is usually not necessary to call this
manually, since it will be called automatically on your first sub()
call.
Topics.start()
Topics.stop
Disconnect from the Topics server and remove all subscriptions.
Topics.stop()
Options
Topics.setDebug
Boolean - set this to true to send logs to stdout. Default false
.
// do this at the top of your script:
Topics.setDebug(true)
Topics.setTimeout
Number - set the connection timeout in ms. Default 10000
.
// do this at the top of your script:
Topics.setTimeout(15000)