Skip to content

Commit c92cfd5

Browse files
Merge pull request #2 from py-package/feature/clients
Feature/clients
2 parents d37b13a + e8b013c commit c92cfd5

File tree

12 files changed

+70
-13
lines changed

12 files changed

+70
-13
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

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

20+
**Masonite Broadcast Server and Client Library**
21+
- [x] [Broadcast Server](https://github.com/py-package/masonite-broadcast-server)
22+
- [x] [broadcast Client](https://github.com/py-package/masonite-broadcast-client)
23+
2024
**Setup**
2125

2226
Install package using pip:
@@ -76,3 +80,14 @@ class YourController(Controller):
7680
}
7781
broadcast.channel(["channel-name"], "event-name", broadcast_data)
7882
```
83+
84+
**Helpers**
85+
86+
List all connected clients:
87+
88+
```python
89+
from socketio_driver.facades import Communicator
90+
91+
communicator.clients() # get list of connected clients
92+
communicator.client(id='client-id') # get client by id, id is basically a socket.io session id
93+
```

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
setup(
77
name="masonite-socketio-driver",
8-
version='2.0.5',
8+
version='2.0.6',
99
packages=[
1010
"socketio_driver",
11+
"socketio_driver.models",
1112
"socketio_driver.config",
1213
"socketio_driver.drivers",
1314
"socketio_driver.providers"
@@ -29,7 +30,7 @@
2930
# 3 - Alpha
3031
# 4 - Beta
3132
# 5 - Production/Stable
32-
"Development Status :: 3 - Alpha",
33+
"Development Status :: 5 - Production/Stable",
3334
# Indicate who your project is intended for
3435
"Intended Audience :: Developers",
3536
"Environment :: Web Environment",

src/socketio_driver/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# flake8: noqa: E501
12
from .providers.socket_provider import SocketProvider

src/socketio_driver/communicator.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
from masonite.configuration import config
2+
import json
13
import redis
24
import msgpack
5+
from .models.socket_client import SocketClient
36

47

58
class Communicator:
69

710
EVENT = 2
811
BINARY_EVENT = 5
912

10-
def __init__(self, opts):
11-
self._opts = opts
13+
def __init__(self, application):
14+
self.config = config("broadcast").get("broadcasts").get("socketio")
15+
self.application = application
16+
17+
self._opts = {
18+
"host": self.config.get("host"),
19+
"port": self.config.get("port"),
20+
}
21+
1222
self._rooms = []
1323
self._flags = {}
1424

@@ -103,5 +113,13 @@ def _createClient(self):
103113
def pubsub(self):
104114
return self._client.pubsub()
105115

116+
def clients(self):
117+
client_keys = self._client.keys(pattern="mbroadcast_users:*")
118+
return [self.user(key) for key in client_keys]
119+
120+
def client(self, id):
121+
client_json_string = self._client.get(id)
122+
return SocketClient(**json.loads(client_json_string))
123+
106124
def authenticate(self):
107125
return True
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# flake8: noqa: E501
12
from .socket_driver import SocketDriver

src/socketio_driver/drivers/socket_driver.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from ..communicator import Communicator
2-
3-
41
class SocketDriver:
52
def __init__(self, application) -> None:
63
self.application = application
@@ -15,12 +12,7 @@ def get_connection(self):
1512
if self.connection:
1613
return self.connection
1714

18-
self.connection = Communicator(
19-
{
20-
"host": self.options.get("host"),
21-
"port": self.options.get("port"),
22-
}
23-
)
15+
self.connection = self.application.make("communicator")
2416

2517
return self.connection
2618

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flake8: noqa: E501
2+
from .communicator import Communicator
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from masonite.facades import Facade
2+
3+
4+
class Communicator(metaclass=Facade):
5+
key = "communicator"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Optional
2+
from ..models.socket_client import SocketClient
3+
4+
class Communicator:
5+
def clients() -> list[SocketClient]:
6+
"""Returns a list of all clients."""
7+
...
8+
def client(id: str) -> SocketClient | None:
9+
"""Returns a client by id."""
10+
...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class SocketClient:
6+
id: str
7+
name: str
8+
address: str

0 commit comments

Comments
 (0)