Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using patterns/wildcards in channel names #283

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ws4redis/redis_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,25 @@ def __new__(cls, value):
elif isinstance(value, list):
if len(value) >= 2 and value[0] == b'message':
return super(RedisMessage, cls).__new__(cls, value[2])
if len(value) >= 3 and value[0] == b'pmessage':
if settings.WS4REDIS_PREFIX:
channel = ":".join(value[2].split(":")[2:])
else:
channel = value[2][value[2].index(":")+1:]
return super(RedisMessage, cls).__new__(cls, json.dumps({channel: value[3]}))
else:
if isinstance(value, (six.string_types, bytearray)):
if value != settings.WS4REDIS_HEARTBEAT:
return six.binary_type.__new__(cls, value)
elif isinstance(value, list):
if len(value) >= 2 and value[0] == 'message':
return six.binary_type.__new__(cls, value[2])
elif len(value) >= 3 and value[0] == 'pmessage':
if settings.WS4REDIS_PREFIX:
channel = ":".join(value[2].split(":")[2:])
else:
channel = value[2][value[2].index(":")+1:]
return six.binary_type.__new__(cls, json.dumps({channel: value[3]}))
return None


Expand Down
5 changes: 4 additions & 1 deletion ws4redis/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def set_pubsub_channels(self, request, channels):
}
self._subscription = self._connection.pubsub()
for key in self._get_message_channels(request=request, facility=facility, **audience):
self._subscription.subscribe(key)
if '*' in key:
self._subscription.psubscribe(key)
else:
self._subscription.subscribe(key)

def send_persisted_messages(self, websocket):
"""
Expand Down