最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the hm: protocol? - Stack Overflow

programmeradmin4浏览0评论

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;

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;
Share Improve this question edited Mar 17, 2017 at 15:30 user3666197 1 asked Mar 15, 2017 at 19:59 NimmiNimmi 1731 gold badge2 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 18

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 plicated 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/smash/get/diva2:706244/FULLTEXT01.pdf https://www.csc.kth.se/~gkreitz/spotifypubsub/spotifypubsub.pdf

发布评论

评论列表(0)

  1. 暂无评论