❤️ Support Masonite

masonite-socketio-driver

Community V2.1.0
12
2
MIT License

pip install masonite-socketio-driver

PyPI issues Python Version GitHub release (latest by date including pre-releases) License star downloads Code style: black

SocketIO Driver

It's an extra broadcast driver support for SocketIO in masonite 4, it extends default masonite broadcast driver to support SocketIO.

Masonite Broadcast Server and Client Library - [x] Broadcast Server - [x] broadcast Client

Setup

Install package using pip:

pip install masonite-socketio-driver

Add provider inside config/providers.py.

from socketio_driver.providers import SocketProvider

PROVIDERS = [
    ...,
    # Application Providers
    SocketProvider,
]

Driver Config Add following configuration inside config/broadcast.py after "pusher": {...},

"pusher": {...},
"socketio": {
    "driver": "socketio",
    "host": env("BROADCAST_HOST", "localhost"),
    "port": env("BROADCAST_PORT", "6379")
},

Add following keys in .env.

BROADCAST_HOST=
BROADCAST_PORT=

Update the broadcast driver value to socketio in .env

BROADCAST_DRIVER=socketio

Example

# broadcast to given channels

from masonite.controllers import Controller
from masonite.broadcasting import Broadcast

class YourController(Controller):

    def your_function(self, broadcast: Broadcast):
        broadcast_data = {
            "message": "Hello World"
        }
        broadcast.channel(["channel-name"], "event-name", broadcast_data)
# broadcast to selected users/clients

from masonite.controllers import Controller
from masonite.broadcasting import Broadcast
from socketio_driver.facades import Communicator

class YourController(Controller):

    def your_function(self, broadcast: Broadcast):
        broadcast_data = {
            "message": "Hello World"
        }
        clients = Communicator.clients()
        ids = [client.socketID for client in clients]
        broadcast.channel(ids, "event-name", broadcast_data)


    def or_another_function(self, broadcast: Broadcast):
        broadcast_data = {
            "message": "Hello World"
        }
        clients = Communicator.clients()
        broadcast.driver("socketio").user(clients[0]).send("event-name", broadcast_data)

Helpers

Facade helpers...

from socketio_driver.facades import Communicator
# List all clients:  
Communicator.clients()
# Get client by sessionID
Communicator.client(id='client-session-id')
# Delete all clients
Communicator.delete_all_clients()
# Delete client by SocketClient instance
Communicator.delete(client)

Last update: