masonite-broadcast-client
v2.0.13
Published
<p align="center"> <img src="https://banners.beyondco.de/Masonite%20Broadcast%20Client.png?theme=light&packageManager=yarn+add&packageName=masonite-broadcast-client&pattern=charlieBrown&style=style_2&description=Broadcast+client+for+masonite+framework
Downloads
80
Readme
Masonite Broadcast Client (WIP)
This is a library providing client side support for the Masonite broadcast driver.
Masonite Broadcast Server and Client Library
Installation
$ npm install masonite-broadcast-client socket.io-client
$ yarn add masonite-broadcast-client socket.io-client
Example
/** Connection */
window.io = require("socket.io-client");
const MasoniteBroadcastClient = require("masonite-broadcast-client");
const broadcast = new MasoniteBroadcastClient({
url: "http://localhost:3000",
broadcastUrl: "http://localhost:8000/broadcast/auth" // optional
});
broadcast.onUserConnected(user => {
console.log(`${user.userID} connected`);
});
/** You can add an extra value in session */
broadcast.setExtra("value", (user) => { // value here must not be complex data types
console.log(user);
})
/** Subscribe to channel */
const subscription = broadcast.subscribe("chat");
/** Broadcast to all */
subscription.emit("your-event", your_data_here)
/** Broadcast to all except the sender */
subscription.broadcast("your-event", your_data_here);
/** Listen for events */
subscription.listen('message', (data) => {
console.log(data);
}).listen('your-event', (data) => {
console.log(data);
});