-
Notifications
You must be signed in to change notification settings - Fork 119
/
channels.py
37 lines (24 loc) · 1.04 KB
/
channels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Notification channels for django-notifs."""
from json import dumps
import pika
from notifications.channels import BaseNotificationChannel
class BroadCastWebSocketChannel(BaseNotificationChannel):
"""Fanout notification channel with RabbitMQ."""
def _connect(self):
"""Connect to the RabbitMQ server."""
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
)
channel = connection.channel()
return connection, channel
def construct_message(self):
"""Construct the message to be sent."""
extra_data = self.notification_kwargs['extra_data']
return dumps(extra_data['message'])
def notify(self, message):
"""put the message of the RabbitMQ queue."""
connection, channel = self._connect()
uri = self.notification_kwargs['extra_data']['uri']
channel.exchange_declare(exchange=uri, exchange_type='fanout')
channel.basic_publish(exchange=uri, routing_key='', body=message)
connection.close()