Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nguyenthanhson's avatar

Playing Sound Without User Interaction

I want to create a sound notification when an event is sent. How can I prevent it from being blocked by the browser?

window.userId = {{ auth()->user()->user_id }};
        const channelName = `laravel_database_chat.${window.userId}`;
        const messageSound = new Audio('{{ asset('clients/assets/mp3/press.wav') }}');

        function playAudio() {
            messageSound.play();
        }
        


        window.Echo.channel(channelName)
            .listen('.MessageEvent', (data) => {
                console.log(data)
                if (data.receiverId === window.userId) {
                    Livewire.dispatch('chat');
                    setTimeout(() => {
                        playAudio();
                    }, 1000);
                }
            });
0 likes
1 reply
martinbean's avatar

How can I prevent it from being blocked by the browser?

@nguyenthanhson You can’t.

If a browser has blocked an auto-playing sound then a browser has blocked it. This usually happens if you try and auto-play media without the user having interacting with the page first, as it can obviously be misused.

If the browser has blocked auto-play, then they’ll usually be a message written to your browser’s console indicating why it was blocked.

Please or to participate in this conversation.