Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the hm:// protocol?

I just looked a bit around in the source code of spotify and found this line of code:

var ALBUM_URI = 'hm://album/v1/album-app/album/';

And I was wondering what this hm:// protocol is. Unfortunately, I didn't find anything on google.

A code-snippet Context taken FROM HERE:

var live = require('spotify-live');
var Cosmos = require('spotify-cosmos-api');

var ALBUM_URI = 'hm://album/v1/album-app/album/';

var formatData = require('../data_formatters');

/**
 * Album model contains all album data
 * @param {string} albumURI The Spotify uri for an album to create a model.
 */
function AlbumModel(albumURI) {
  this.albumURI = albumURI;
  this.album = live(albumURI);
}

AlbumModel.prototype.init = function(callback) {
  this.callback = callback;

  var requestURI = ALBUM_URI + this.albumURI + '/desktop';

  Cosmos.resolver.get(requestURI, this.prepareData.bind(this));
};

AlbumModel.prototype.prepareData = function(error, data) {
  if (error) {
    var errorStatusCode = error.response ? error.response.getStatusCode() : 500;

    var simplifiedStatusCode = 400;
    if (400 <= errorStatusCode && errorStatusCode <= 410) {
      simplifiedStatusCode = 400;
    } else if (500 <= errorStatusCode && errorStatusCode <= 503) {
      simplifiedStatusCode = 500;
    }

    this.callback(simplifiedStatusCode, {});
    return;
  }

  var albumData = data.getJSONBody();
  albumData = formatData(albumData);

  if (!this.album.get('rows')) {
    this.album.update(albumData);
  }

  this.callback(null, albumData);
};

module.exports = AlbumModel;
like image 491
Nimmi Avatar asked Mar 15 '17 19:03

Nimmi


1 Answers

hm is short for hermes, a protocol used internally between servers at Spotify. It is basically zeromq with a protobuf envelope with some defined headers.

So, kind of like HTTP define verbs and structure on-top of TCP, Hermes define verbs and structure on-top of zeromq. It is used for HTTP-like Request/Response as well as Publish/Subscribe. For instance, in the example you found, a client request data about an album and waits for a response. Another example could be a client subscribing to events about a playlist. The moment someone publishes a change to the playlist, the client will know.

It gets extra complicated in the example you found. It seems to be a javascript snippet found in the Spotify web player. Javascript in the browser can of course not talk this protocol, so because you see the uri there, it means there is some kind of tunneling going on as well.

In one sense, it can do more than HTTP, but in another sense, it is much simpler because of the limited use. It was built many years ago, before HTTP/2 and grpc. It is still used heavily at Spotify.

I found a thesis work and an industry article that mentioned hermes in more detail.

http://www.diva-portal.org/smash/get/diva2:706244/FULLTEXT01.pdf https://www.csc.kth.se/~gkreitz/spotifypubsub/spotifypubsub.pdf

like image 122
jooon Avatar answered Oct 09 '22 00:10

jooon