Bitcoin Forum
November 21, 2017, 08:22:46 AM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: MtGox Streaming API Question  (Read 1189 times)
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 09:20:54 AM
 #1

Working on a trade ticker. I know there are a good chunk of these already but I am working on a personal one for me and a few friends. Figure my server would be able to handle me and 2 other people better than trying to squeeze in with 5000 other people.

Anywho... was going to post in https://bitcointalk.org/index.php?topic=5855.340 but I guess I have to do it here for now

http://www.introspectivity.com/TradeThatCoin/ - The product of a few hours worth of work today (and learning how to use WebSockets, small pain in the ass but fairly easy!). I am noticing that however I don't stay connected for long with the mtgox streaming api on my site and that other websites like bitcoin.clarkmoody.com connection stay alive a lot longer.

Currently in my code I am unsubscribed from 2 channels, would this have an effect? Maybe because things aren't getting pushed as much? If this is the case I thought websockets was supposed to just remain alive?

I am pretty confused as far as that goes. Not really sure what to do to fix that.

Edit: P.S Go easy on me as far as looks... This is no where near the finished version. Just needed a console to look @ output while I hammer all the bugs out then drastically change the layout (gonna be more for mobile - Clarks unfortunately doesn't have a mobile version)
1511252566
Hero Member
*
Offline Offline

Posts: 1511252566

View Profile Personal Message (Offline)

Ignore
1511252566
Reply with quote  #2

1511252566
Report to moderator
1511252566
Hero Member
*
Offline Offline

Posts: 1511252566

View Profile Personal Message (Offline)

Ignore
1511252566
Reply with quote  #2

1511252566
Report to moderator
Join ICO Now Coinlancer is Disrupting the Freelance marketplace!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1511252566
Hero Member
*
Offline Offline

Posts: 1511252566

View Profile Personal Message (Offline)

Ignore
1511252566
Reply with quote  #2

1511252566
Report to moderator
1511252566
Hero Member
*
Offline Offline

Posts: 1511252566

View Profile Personal Message (Offline)

Ignore
1511252566
Reply with quote  #2

1511252566
Report to moderator
1511252566
Hero Member
*
Offline Offline

Posts: 1511252566

View Profile Personal Message (Offline)

Ignore
1511252566
Reply with quote  #2

1511252566
Report to moderator
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 09:48:15 AM
 #2

bump
andr3w321
Jr. Member
*
Offline Offline

Activity: 32


View Profile WWW
April 09, 2013, 10:07:56 AM
 #3

I am having the same problem but using socket.io... the newbie restriction here is incredibly frustrating.  I read the whole Mtgox API thread(though not while logged in) and didn't find an answer.  Here's my code which seems to work, but I can't figure out how to unsubscribe from channels.

    <script src="/js/socket.io.min.js"></script>
    <script>
       var mtgox = io.connect("http://socketio.mtgox.com:80/mtgox?Currency=USD");
       var mtgox = io.connect("http://socketio.mtgox.com:80/mtgox?Currency=USD");
       mtgox.on("connect", function(data) {
         console.log('connected');
          mtgox.on("message", function(msg) {
                        // uncomment below to see depth and trade channels which I don't need
             // console.log(msg);
         if(msg.channel == "d5f06780-30a8-4a48-a2f8-7ed181b4a13f" && msg.origin === "broadcast") console.log(msg.ticker.last.display_short);

          });
       });
    </script>

I've tried the below at various places, but haven't gotten that to work.
       //var unsub = {"op": "unsubscribe", "channel": "d5f06780-30a8-4a48-a2f8-7ed181b4a13f"};
       //mtgox.emit("broadcast", unsub);
       //mtgox.send(unsub);

http://bitcoinrichlist.com
BTC: 13gpMizSNvQCbJzAPyGCUnfUGqFD8ryzcv
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 10:36:16 AM
 #4

Trying to work on your code right now bud - should have a solution for you and reasoning here in like 15
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 10:45:54 AM
 #5

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://socketio.mtgox.com/socket.io/socket.io.js"></script>

</head>

<body>
<script>
   
var mtgox = io.connect("http://socketio.mtgox.com/mtgox");

mtgox.on("connect", function(e) {

console.log('connected');

mtgox.on("message", function(msg) {

console.log(msg.channel);
});

});
</script>
</body>
</html>

This so far is listing the channels as they come in. Everytime the server sends out one of the channels (ticker, depth, or trades) it lists the channel in the console. I am now going to try and do the other part you were working on
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 11:13:42 AM
 #6

Ok so here is the code you are looking for (and one of my homemade functions, enjoy). This works and should show you what you want.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://socketio.mtgox.com/socket.io/socket.io.js"></script>

</head>

<body>
<div id="output">
</div>
<script>
    
var mtgox = io.connect("http://socketio.mtgox.com/mtgox");

var ticker = "d5f06780-30a8-4a48-a2f8-7ed181b4a13f";
var trade = "dbf1dee9-4f2e-4a08-8cb7-748919a71b21";
var depth = "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe";

var output = document.getElementById("output");

mtgox.on("connect", function(e) {

LogTextToConsole(output, "Connected");

mtgox.send({
"channel":depth,
"op":"unsubscribe"
});

LogTextToConsole(output, "Unsubscribed from Depth");

mtgox.on("message", function(msg) {

if( (msg.channel == ticker) && (msg.origin == "broadcast") ) {
LogTextToConsole(output, msg.ticker.last.display_[Suspicious link removed]String());
}

});

});

function LogTextToConsole(ob, inc, b, c) {

// Create Div Container for Object and assign Class Name
var divText = document.createElement("div");
var defaultColor = '#FFFFFF';
c = c || defaultColor;
b = b || false;

divText.className = "divStringHolder";

// Check to see if the object is a string, if it is display it.
if (typeof inc == "string") {

// Assign text to div
divText.textContent = inc;

// If bold, bold it
if (b == true) {

// Adds bold if requested
divText.style.fontWeight = "bold";
}

if (c != defaultColor) {
// Changes from the default to something specified
divText.style.backgroundColor = c;
}
else {
// Default Notification Color
divText.style.backgroundColor = defaultColor;
}

/* IMPORTANT: Inserts before the first child therefore creating a
* top to bottom effect where the newest information is on the top
* and the oldest is on the bottom */
ob.insertBefore(divText, ob.firstChild);
}

}
</script>
</body>
</html>

http://www.introspectivity.com/TradeThatCoin/testcode.php

What I think was going wrong for you was where you had your script. I've always seen them at the bottom of the body for sockets, that's where I've been putting mine anywhere with no problems.

also biggest thing they don't specifically tell you

Code:
mtgox.send({
     "channel":depth,
     "op":"unsubscribe"
});

That is how you unsubscribe and subscribe to channels. If you need help subscribing to the lag channel later to late me know Smiley I didn't realize the socket io and the websocket were that close in syntax

Edit: had to change some of the leftover code from mine
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 11:30:47 AM
 #7

ok this

Code:
LogTextToConsole(output, msg.ticker.last.display_[Suspicious link removed]String());

don't know why this keeps saying that but it is supposed to be display_short and then .toString()

edit: double edit, damn I gotta hit the sack soon I am tired. Messing up

Code:
ob.insertBefore(divText, ob.firstChild);
ob.firstChild was console.firstChild i switched it to ob.firstChild (what it is supposed to be).

now all should work fine... I hope
andr3w321
Jr. Member
*
Offline Offline

Activity: 32


View Profile WWW
April 09, 2013, 11:44:06 AM
 #8

You sure you're not receiving depth channel info still?  Here's the code I tried.

Code:
    <script src="http://socketio.mtgox.com/socket.io/socket.io.js"></script>
    <script>

var mtgox = io.connect("http://socketio.mtgox.com:80/mtgox");

var ticker = "d5f06780-30a8-4a48-a2f8-7ed181b4a13f";
var trade = "dbf1dee9-4f2e-4a08-8cb7-748919a71b21";
var depth = "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe";


mtgox.on("connect", function(e) {

console.log("Connected");

mtgox.send({
"channel":depth,
"op":"unsubscribe"
});

mtgox.on("message", function(msg) {
console.log(msg);

if( (msg.channel == ticker) && (msg.origin == "broadcast") ) {
console.log(msg.ticker.last.display);
}

});

});

    </script>

Output from console.  In bold is the server reply to my mtgox.send() request.

Connected
Object {op: "subscribe", channel: "dbf1dee9-4f2e-4a08-8cb7-748919a71b21"}
Object {op: "subscribe", channel: "d5f06780-30a8-4a48-a2f8-7ed181b4a13f"}
Object {op: "subscribe", channel: "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe"}
Object {op: "remark", success: false, message: "Unknown command", debug: Object}
Object {channel: "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe", channel_name: "depth.BTCUSD", op: "private", origin: "broadcast", private: "depth"…}
Object {channel: "d5f06780-30a8-4a48-a2f8-7ed181b4a13f", channel_name: "ticker.BTCUSD", op: "private", origin: "broadcast", private: "ticker"…}
$206.00002
Object {channel: "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe", channel_name: "depth.BTCUSD", op: "private", origin: "broadcast", private: "depth"…}
Object {channel: "dbf1dee9-4f2e-4a08-8cb7-748919a71b21", channel_name: "trade.BTC", op: "private", origin: "broadcast", private: "trade"…}
 

http://bitcoinrichlist.com
BTC: 13gpMizSNvQCbJzAPyGCUnfUGqFD8ryzcv
Trokair
Newbie
*
Offline Offline

Activity: 12


View Profile
April 09, 2013, 12:01:06 PM
 #9

Hrmmmm.

Crap. I'm not sure then. I've been up all night and it's 8:02 I can barely stay awake Sad I'll try again in a few hours
andr3w321
Jr. Member
*
Offline Offline

Activity: 32


View Profile WWW
April 09, 2013, 09:54:37 PM
 #10

I think I'm going to abandon connecting to their socket directly as it seems unreliable and use my server as a proxy to communicate with mtgox api.

On server side, every 15-30 seconds my server will send an ajax request to
https://data.mtgox.com/api/2/BTCUSD/money/ticker
If the price has changed I will update my entry in my database to reflect the new price and emit a socket.io event of a price update

On client side, the user will initially ping my server db for the price info and then connect to my server using socket.io and wait for price updates.

http://bitcoinrichlist.com
BTC: 13gpMizSNvQCbJzAPyGCUnfUGqFD8ryzcv
andr3w321
Jr. Member
*
Offline Offline

Activity: 32


View Profile WWW
April 10, 2013, 10:05:26 PM
 #11

Finally figured it out.  The code from listentobitcoin.com really helped https://github.com/MaxLaumeister/Listen-To-Bitcoin/blob/master/socket.js

I was just missing wrapping JSON.stringify() around my send message.

Using this is much easier though: http://blockchain.info/api/exchange_rates_api

But if you do go the socket to mtgox route, it looks like a plain reconnecting websocket like listen to bitcoin switched to is more reliable than the socket.io version.


http://bitcoinrichlist.com
BTC: 13gpMizSNvQCbJzAPyGCUnfUGqFD8ryzcv
btcsharp
Newbie
*
Offline Offline

Activity: 5


View Profile
April 29, 2013, 06:22:20 PM
 #12

Quote
That is how you unsubscribe and subscribe to channels. If you need help subscribing to the lag channel later to late me know Smiley I didn't realize the socket io and the websocket were that close in syntax

Edit: had to change some of the leftover code from mine

He didn't ask, so I'll ask... How do you subscribe to the lag channel? Thanks!

yucca
Full Member
***
Offline Offline

Activity: 217



View Profile WWW
April 29, 2013, 11:10:34 PM
 #13

Quote
That is how you unsubscribe and subscribe to channels. If you need help subscribing to the lag channel later to late me know Smiley I didn't realize the socket io and the websocket were that close in syntax

Edit: had to change some of the leftover code from mine

He didn't ask, so I'll ask... How do you subscribe to the lag channel? Thanks!



+1

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!