# Using Django channel and Rest without signals.

**URL:** <https://forum.djangoproject.com/t/using-django-channel-and-rest-without-signals/22240>\
**Category:** Async/Channels\
**Created:** [July 12, 2023, 11:28am UTC](https://forum.djangoproject.com/t/using-django-channel-and-rest-without-signals/22240 "2023-07-12T11:28:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [July 12, 2023, 11:28am UTC](https://forum.djangoproject.com/t/using-django-channel-and-rest-without-signals/22240/1 "2023-07-12T11:28:31Z")

</div>

Hi, I am using Django Channels and RestFramework to build a back-end which I can send real time data to a front-end, and my intention is when the database is updated, the front-end endpoint to be updated without it being refreshed, here is my Code so far:

#consumer.py

```auto
class MyConsumer(AsyncWebsocketConsumer):
    
    async def connect(self):
        await self.channel_layer.group_add('myCons',self.channel_name)
        await self.accept()

        boat = await self.get_boat(1)
        boat_serializer = BoatSerializer(boat)
        boat_data = boat_serializer.data

        await self.send(json.dumps({"boat": boat_data,}))

    async def boat_updated(self, event):
        boat_data = event['boat']
        await self.send(json.dumps({"boat": boat_data}))

    @database_sync_to_async
    def get_boat(self,pk):
        boat = Boat.objects.get(pk=pk)
        return boat
      
@receiver(post_save, sender=Boat)
def boat_post_save(sender, instance, **kwargs):
    boat_data = BoatSerializer(instance).data

    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.group_send)('myCons', {
        'type': 'boat.updated',
        'boat': boat_data,
    })

```

And here is my front-end code:  
#home.html (can be any front-end client)

```auto
  const chatSocket = new WebSocket(
      'ws://'
      + window.location.host
      + '/ws/myconsumer/'
  );
  
  chatSocket.onmessage = function(e) {
    const data = JSON.parse(e.data);

      const messageElement = document.getElementById('messageContainer');
      messageElement.innerHTML = `
        <h1>My ${data.boat.name}</h1>
        <p>contact: ${data.boat.contact}</p>
        <p>Type: ${data.boat.type}</p>
        <p>Serial: ${data.boat.serial}</p>
      `;
};

```

the code works as expected, However my concern is, I am implementing this with the help of django Signals, is there a way I can accomplish this without using signals. Or can anyone please point me to a right direction,

thanks.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 12, 2023, 11:56am UTC](https://forum.djangoproject.com/t/using-django-channel-and-rest-without-signals/22240/2 "2023-07-12T11:56:32Z")

</div>

> [@matteyahmed](#):
>
> is there a way I can accomplish this without using signals.

Yes, you can send a message through the Channel layer from Django to the consumer. See [Channel Layers — Channels 4.0.0 documentation](https://channels.readthedocs.io/en/stable/topics/channel_layers.html), and in particular, the section on [Using Outside of Consumers](https://channels.readthedocs.io/en/stable/topics/channel_layers.html#using-outside-of-consumers)

You’ll have to ensure that this message gets sent anywhere the data gets changed, but it does allows you to handle those situations not covered by a signal.

So depending upon the nature of these updates and possible relationships between models, this may be a more robust solution, but at the expense of there being more work involved.

---

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [July 12, 2023, 1:55pm UTC](https://forum.djangoproject.com/t/using-django-channel-and-rest-without-signals/22240/3 "2023-07-12T13:55:00Z")

</div>

I dont mind a bit more work, if it is possible. Thanks for the pointers.
