Backend extension that listens for streamTitleChanged events

In a running mopidy service, I would like to keep a history list (in memory or perhaps file) of received streamTitles.

My idea is to create a minimal backend extension that listens for streamTitleChanged events.

The backend could then perhaps send a custom event/message to my frontEnd with the latest n titles, after each new received title.

Is what I’m trying to do possible?

Can I register both a backEnd and a frontEnd in one package or is that a bad idea?

After reviewing code here and there, I’m a bit lost. Can I get some pointers?

For context - I’m new to extension development. Altering a cloned MusicBox is my only experience. I’m currently having the frontend website (browser) HTTP POST back received titles to the frontEnd extension, but that’s of course a bit of a hack.

A bit of guidence in the right direction would be much appreciated. Thanks.

You can listen for streamTitleChanged events directly in your frontend. Simply implement a stream_title_changed function in your frontend, similar to what Mopidy-Scrobbler does for the two track playback events it’s listening to.

Oh, is it that simple? Thank you, that worked!
But can I filter them and send them with a custom event to the http client (the browser)?

In other words, can I receive in javascript:

mopidy.on(“event:filtered_titles_changed”, function(data) {
...
});

If not, I’ll just receive the original event and fetch the filtered titles from theFrontEnd’s http server.

But a custom event would be nice.

Ok, for those interested, this is how I got custom events to work:

TLDR;

A custom event send like ‘stream_history_changed’ will be received as ‘event:streamHistoryChanged’.

Long-winded:

Your FrontEnd will likely inherit from CoreListener:

class MyFrontend(pykka.ThreadingActor, core.CoreListener):

…and thus expose the send function, which can be used like this:

self.send('stream_history_changed', data={
    'event': 'stream_history_changed',
    'data': 'todo...'
})

To solve the receiving part, I first found how to listen to ALL events in JavaScript:

mopidy.on(console.log.bind(console));

Turns out in JS, the snake case event name is received as a CamelCase one!

So, this listener is the one that works for the above event:

mopidy.on("event:streamHistoryChanged", function(data) {
    console.log("Stream history changed:", data);
});

Since the CoreListener.send() function seems to only want a pre-defined set of events, this is probably a bit of a hack.

Presumably you see a load of “Triggering event failed” messages in your log?

Also, I am surpsied you need to include 'event': 'stream_history_changed' in your data. Doesn’t it also work with

self.send('stream_history_changed', data={
    'data': 'todo...'
})

and then

mopidy.on("streamHistoryChanged", function(data) {
    console.log("Stream history changed:", data);
});

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.