Create a sound on my browser (notification)

Hello, I have a small question, I have been browsing to find a solution to create a sound notification on my browser, when I receive data in my database via a webshook, I would like to play a sound on my browser when there is new data in my database, but I have not found anything if someone can direct me to a solution a tool that can do this. Thanks!

I’m not really sure what information you’re really looking for here.

Generally speaking, handling audio is something done in the browser with some JavaScript.

Depending upon the range of browsers you need to support, you’ve got a number of options:

I know you’re using websockets, so you could probably just send a notification through the socket to tell your JavaScript to play a sound that has already been loaded.

Super thank you very much for this track, :slightly_smiling_face:

I just raised a problem on my websocket, indeed when I send a post in my app, I filter the data received and I associate them to a user, and so I made several tests, for example if I am connected on user1 and I send a post which is linked to another user, for example user2 I receive the data of user2 on user1, it’s only when I reload the page that each data linked to the user is displayed correctly, in conclusion when I post a data it’s displayed on all the pages of the users and it’s only when I reload the page that the data come back to normal.

(I create another topic for that? or can we stay here ?)

Your choice, I don’t think it’s a problem either way.

okok, but then when do you think?

in my opinion it comes from the view, when I associate my data with an ascount

I’d have to see the consumers and how you’re sending the updates.

If data is getting sent to all connected users when it should only be sent to one user, then you’re not handling the updates appropriately.

its my

class HubriseOrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):

    def get_queryset(self, **kwargs):
        return HubriseOrder.objects.filter(ascount=self.scope["user"]).exclude(
        status='En attente').exclude(
        status='Livraison échouer').order_by('created_at')

    serializer_class = HubriseOrderSerializer
    permissions = (permissions.AllowAny,)

    async def connect(self, **kwargs):
        await self.model_change.subscribe()
        await super().connect()

    async def model_change(self, message, observer=None, **kwargs):
        await self.send_json(message)

    def model_serialize(self, instance, action, **kwargs):
        return dict(data=HubriseOrderSerializer(instance=instance).data, action=action.value)

I forgot this my

from django.urls import re_path

from . import consumers

# websocket_urlpatterns = [re_path(r"^ws/$", consumers.PostConsumer.as_asgi())]

websocket_urlpatterns = [
    re_path(r"^ws/$", consumers.HubriseOrderConsumer.as_asgi())]

I’m guessing this “model_observer” is a signal that gets notified whenever a model is changed. That means that yes, every model being changed is going to trigger this signal for every consumer.

uh, you mean for each user instead?

No, if a user doesn’t have an active websocket, they would not see the updates.

If you’ve got 20 users, and 10 of them are logged on and connected, you have 10 active consumers. Changing any instance of any model being monitored (HubriseOrder?) is going to fire that signal for all 10 consumers.

ah ok you teach me something, I didn’t know it worked like that, what do you recommend to send it to only one user then?

I don’t know enough about your applications architecture to give you any specific advice, but my typical advice against using signals except as a last resort is well documented in the forum here.

Generally speaking, you’ve got a couple different ways to handle this. Your choice would depend upon what information you have at which points in time to identify who should receive a message.

Personally, I use django-channels-presence (patched to work with current Django) for this type of situation. Each user who connects is joined to a Room, which works like a Channels group, except it tracks who is connected. You can then query the Room model to get the specific channel for a connected user and direct a message to them.

there is just the documentation for the installation ? I have searched but I can’t find any doc to configure this

just install it? :slight_smile: django-channels-presence

The full docs are Usage — django-channels-presence 1.0.0 documentation

I finished configuring my consumer, but when I run it, I get this error I think there is a compatibility problem with another package.

PS C:\Users\Abder-Rahmanhe\Desktop\Django\Orderline APP\OrderLineApp> python runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\apps\", line 245, in create
    app_module = import_module(app_name)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\importlib\", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rooms'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\", line 1016, in _bootstrap_inner
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\", line 953, in run 
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\management\commands\", line 125, in inner_run
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\management\", line 398, in execute
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\", line 24, in setup
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\apps\", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\Abder-Rahmanhe\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\apps\", line 247, in create
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'rooms'. Check that '' is correct.

That’s interesting, I’ve never seen it throw that error before. (I’ve never tried to use it in Windows before, either.)

Did you fix the syntax error in channels_presence.signals ?

I don’t see in the channels_presence.signals logs, where is it exactly?