1.1.0 • Published 2 years ago
@sqwad/direct-flow v1.1.0
Sqwad DirectFlow Client Javascript / Typescript library
Instant bidirectional interactions made easy !
Note: we use socket.io library for client layer with auto-reconnect and WebSocket / Long polling usage.
The library is coded on typescript so all types are defined, except for custom ones.
CDN Use
<script src="https://cdn.jsdelivr.net/npm/@sqwad/direct-flow" crossorigin="anonymous"></script>
<script>
const d = new DirectFlow('your-uuid', 'client-key')
d.broadcast('Hello world')
d.onMessage((message) => {
console.log(message)
})
</script>Installation
yarn add @sqwad/direct-flowor
npm install @sqwad/direct-flowJavascript Usage
<script type="module">
import {DirectFlow} from '@sqwad/direct-flow'
const d = new DirectFlow('your-uuid', 'client-key')
d.broadcast('Hello world')
d.onMessage((message) => {
console.log(message)
})
</script>Event driven usage
<script type="module">
import {DirectFlow} from '@sqwad/direct-flow'
const d = new DirectFlow('your-uuid', 'client-key')
d.broadcast('Hello world')
// Instantiate all event dispatchers
d.onMessage()
addEventListener(DirectFlowEventType.GLOBAL, (message) => console.log(message))
</script>Methods
connect: connect to server (not needed, connected in constructor)release: disconnect from serverbroadcast: send message to anyone except us (can be string or object)send: broadcast aliassendTo: send to specific clientsendToChannel: send to channel on server (can be string or object)rawSend: send any data to server, but need to be an objectsubscribe: listen to one or more specific channel (string or object)unsubscribe: stop listening for one or more specific channel (string or object)onMessage: action to do when receiving message, parameter is optional and instantiate event dispatchers, you should call once only.