Skip to content

Instantly share code, notes, and snippets.

@jacoor
Last active January 11, 2017 16:23
Show Gist options
  • Save jacoor/84cb03a9dd93a76fb3bc384531110c14 to your computer and use it in GitHub Desktop.
Save jacoor/84cb03a9dd93a76fb3bc384531110c14 to your computer and use it in GitHub Desktop.
Simple Websocket Receiver using django
# update your app settings.py with RedisSubscriber:
WS4REDIS_SUBSCRIBER = 'your_app.your_module.subscriber.RedisSubscriber'
# -*- coding: utf-8 -*-
"""
Very simple RedisSubscriber class.
It only receives the message and prints out stuff. Your job is to implement it properly.
Suggestion: use celery job to make it freaking fast.
Assumptions:
- django-websocket-redis installed
"""
from ws4redis.subscriber import RedisSubscriber
class RedisSubscriber(RedisSubscriber):
def publish_message(self, message, expire=None):
try:
# custom code goes here
# for efficiency think about using celery to do the job - has to be freaking FAST!
print ('message received!')
print ('message!')
except Exception:
# I mean it, catch everything, log it if needed but do catch everything
pass
return super(RedisSubscriber, self).publish_message(message, expire)
Testing receiving WS is a bit of a problem as you need to send WS from browser to the server.
Assumming you already have broadcast from server connection available use method described here:
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment