How can I implement a system that can "push" messages to clients when necessary? Basically, what I need is the ability to "push" text to Python clients from a Python server. The text will then be parsed to actions that will take care at the client side (I already know how to do this thing, once an 'action-text' is received). I can check every couple of seconds for new 'action-texts' that are waiting -- it's just I don't think it's reliable and scalable to thousands of clients. The real-time thing is very important here.
Any suggestions, please?
You can use redis publish subscribe model more here.
Redis is highly scalable and fast.
Example: (From https://github.com/andymccurdy/redis-py/blob/master/tests/pubsub.py)
import redis
import unittest
class PubSubTestCase(unittest.TestCase):
def setUp(self):
self.connection_pool = redis.ConnectionPool()
self.client = redis.Redis(connection_pool=self.connection_pool)
self.pubsub = self.client.pubsub()
def tearDown(self):
self.connection_pool.disconnect()
def test_channel_subscribe(self):
self.assertEquals(
self.pubsub.subscribe('foo'),
['subscribe', 'foo', 1]
)
self.assertEquals(self.client.publish('foo', 'hello foo'), 1)
self.assertEquals(
self.pubsub.listen().next(),
{
'type': 'message',
'pattern': None,
'channel': 'foo',
'data': 'hello foo'
}
)
self.assertEquals(
self.pubsub.unsubscribe('foo'),
['unsubscribe', 'foo', 0]
)
def test_pattern_subscribe(self):
self.assertEquals(
self.pubsub.psubscribe('fo*'),
['psubscribe', 'fo*', 1]
)
self.assertEquals(self.client.publish('foo', 'hello foo'), 1)
self.assertEquals(
self.pubsub.listen().next(),
{
'type': 'pmessage',
'pattern': 'fo*',
'channel': 'foo',
'data': 'hello foo'
}
)
self.assertEquals(
self.pubsub.punsubscribe('fo*'),
['punsubscribe', 'fo*', 0]
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With