Help us understand the problem. What is going on with this article?

【最短サンプル】ブラウザのJavaScriptで、bitFlyerのAPIにWebSocket接続してビットコイン最新価格を次々と受け取る

以下をindex.htmlとして保存して、ブラウザで開けばconsoleに現在価格が次々と出ます。

index.html
<script>
    var sock = new WebSocket('wss://ws.lightstream.bitflyer.com/json-rpc');

    sock.addEventListener("open", e => {
        sock.send('{"method": "subscribe","params": {"channel": "lightning_ticker_FX_BTC_JPY" }}');
    });
    sock.addEventListener("message", e => {
        var json = JSON.parse(e.data).params.message;
        console.log(json);
    });
</script>

1.bitFlyerにWebSocket接続して
2.価格のチャンネルを購読(subscribe)すれば
3.次々と最新価格が送られて来て、そのたびにmessageイベントが発生する

という動きになります。

公開されているAPIですので、bitFlyer契約者でなくても試せます。

例ではビットコインFXの価格を取得しています。詳しくは以下で。
https://bf-lightning-api.readme.io/docs/realtime-ticker

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away