# Simple event listener

**URL:** <https://discourse.mopidy.com/t/simple-event-listener/557>\
**Category:** Misc\
**Created:** [January 26, 2015, 9:39pm UTC](https://discourse.mopidy.com/t/simple-event-listener/557 "2015-01-26T21:39:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Saffrey](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Peter\_Saffrey](https://discourse.mopidy.com/u/Peter_Saffrey)\
**Post date:** [January 26, 2015, 9:39pm UTC](https://discourse.mopidy.com/t/simple-event-listener/557/1 "2015-01-26T21:39:40Z")

</div>

I’m using mpd on a Raspberry Pi as a living room jukebox. It’s connected to a speaker by a headphone cable, but the speaker is also capable of Bluetooth. I would like to listen to events that happen to mpd and trigger actions - such as connecting to the speaker with Bluetooth when somebody presses play. This would allow me to site the Pi anywhere in the room, which is useful (as well as being quite cool 😄 I haven’t been able to find good documentation on doing this with mpd, but mopidy looks more promising because of the events support in the Python API.

Does anybody have a minimal example of a mopidy event listener in Python I can use as a starting point?

---

<div class="post-metadata">

**Author:** ![liamw9534](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@liamw9534](https://discourse.mopidy.com/u/liamw9534)\
**Post date:** [January 27, 2015, 12:31am UTC](https://discourse.mopidy.com/t/simple-event-listener/557/2 "2015-01-27T00:31:12Z")

</div>

Have a look in my github account. You will find Mopidy extensions under development for managing Bluetooth devices which also export events over HTTP web socket.

There is also the option of automatically connecting to a speaker once it is discovered.

Alternatively, without using any extensions, just use GStreamer sbcenc and a2dpsink plugins directly in your audio output configuration setting - this will thenconnect to your BT speaker whenever you play.

---

<div class="post-metadata">

**Author:** ![jaedb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/jaedb/32/2340_2.png) [@jaedb](https://discourse.mopidy.com/u/jaedb)\
**Post date:** [September 6, 2016, 8:30pm UTC](https://discourse.mopidy.com/t/simple-event-listener/557/3 "2016-09-06T20:30:00Z")

</div>

Hi Liam, can you please post an example of the code Peter was after? Peter did you have any luck on this?

I’m interested in achieving this also to build radio functionality into the [Spotmop](https://github.com/jaedb/spotmop) frontend.

---

<div class="post-metadata">

**Author:** ![liamw9534](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@liamw9534](https://discourse.mopidy.com/u/liamw9534)\
**Post date:** [September 13, 2016, 1:41pm UTC](https://discourse.mopidy.com/t/simple-event-listener/557/4 "2016-09-13T13:41:08Z")

</div>

The extensions were implemented on a mopidy fork I made 2 years ago: [https://github.com/liamw9534/mopidy/commits/liamw\_mopidy\_dev;](https://github.com/liamw9534/mopidy/commits/liamw_mopidy_dev;) this includes other changes as well to the audio backend for allowing dynamic sink addition/removal.

I can’t really remember much about it now but the main changes are in this commit: [https://github.com/liamw9534/mopidy/commit/278eb62d50b68cd0376041d726e9b6d7087f6755#diff-f3138e0ab17ebf1710d15e3e68a6ee65](https://github.com/liamw9534/mopidy/commit/278eb62d50b68cd0376041d726e9b6d7087f6755#diff-f3138e0ab17ebf1710d15e3e68a6ee65)

The basic idea is that any extension that sub-classes service.Service is registered as a “service” and is able to propagate events and have its remote procedures called via websocket interface using jsonrpc.

An example extension that uses this is [https://github.com/liamw9534/mopidy-btmanager/blob/master/mopidy\_btmanager](https://github.com/liamw9534/mopidy-btmanager/blob/master/mopidy_btmanager). If you look at [actor.py](http://actor.py) you can see various cases of events being sent using the service.ServiceListener.send() method. Otherwise, all the methods (not decorated with @private\_method) are callable over JSON RPC.

---

<div class="post-metadata">

**Author:** ![Peter\_Saffrey](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Peter\_Saffrey](https://discourse.mopidy.com/u/Peter_Saffrey)\
**Post date:** [September 13, 2016, 2:43pm UTC](https://discourse.mopidy.com/t/simple-event-listener/557/5 "2016-09-13T14:43:08Z")

</div>

I ended up using Bluetooth/ALSA integration. I don’t remember the exact details, but something like this:

[https://wiki.debian.org/Bluetooth/Alsa](https://wiki.debian.org/Bluetooth/Alsa)

This is actually independent of the player software.

I also moved from mpd to Squeezebox, largely because of the synchronisation/multi-device support but the apps are nice too - quite a few streaming services are available out of the box.

Peter
