Your Steemjs API connection is broken now .. and how to fix it

in #utopian-io7 years ago (edited)

image.png

I woke up on this beautiful Monday morning, sipping through my cup of tea (or coffee for hard core devs), testing out my steem APIs code, when all of a sudden, nothing works...tea spilled !!

What am I messing up? If you've been coding for a while now, you would know leaving a tiny detail as trivial as a semicolon, could break things up.. yet my code / plugin was working 2 days ago .. what gives?!

Troubleshooting steem API connection issues

So I started digging into the issue. My steem API connection plugin, similarly to many of yours, are currently using the latest steemjs script which can be utilized via including the following script into your code:
<script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>
If we were to grab the number of accounts on steemit at the moment, as one of my plugins does at the moment, we would be simply calling the following code (also available on my gk steemit info wordpress plugin for ease of integration into your wordpress site: ):

steem.api.getAccountCount(function(err, result) {
    //result now contains the number of accounts. Display this into the input box
    $('#steemit_accounts').text(result);
    console.log(err, 'account count:'+result);
});

and this basically failing! Checking onto our javascript console, we are now getting a "WS" connection error. This might show up a bit late pending the timeout period for connecting to the WS connection
image.png
So apparently the issue is not a code bug on our side, there is a connection issue to steemd.steemit.com

Changes to steem API: dropping steemd

I remembered that two months back, steemit dev team announced that steemd.steemit.com is going to be deprecated, and what this basically means for non-dev-terminology guys, it will be dropped in favor of new and better functionality, provided now by new and improved connection to https://api.steemit.com
Announcement came through under this post Update your STEEM apps! Big changes coming for 3rd party developers
And now checking onto the official @steemdev account for latest announcements, indeed the plan was to retire steemd on Jan 6, and today is .. Jan 8, so now we know why the code is not functional! Last chance to update - steemd.steemit.com will be retired on January 6

Making our API calls work again!

So now that we know what the issue is, we need to attempt to fix it.
The move requires dropping the use of the old WS (web socket) endpoint, and now moving into a http/jsonrpc endpoint instead. Steemjs did not make the change on its own yet, so we need to figure a clean way to fix this until it gets updated on steemjs side.
Digging into steemjs documentation, I found that it provides proper method for adjusting the endpoint to which you are connecting, this is provided via performing a call to steem.api.setOptions. Check documentation here.
So technically, before running our normal calls to steemjs library, we would need to instruct it to use the new API endpoint, via including a call the setOptions function, as follows:
steem.api.setOptions({ url: 'https://api.steemit.com' });
Hence our new code will now look something like this:

steem.api.setOptions({ url: 'https://api.steemit.com' });
steem.api.getAccountCount(function(err, result) {
    //result now contains the number of accounts. Display this into the input box
    $('#steemit_accounts').text(result);
    console.log(err, 'account count:'+result);
});

Did it work?

Here is a screenshot from my console. You can see that the basic user count function is coming back with results, and we are now at around 580k users at steemit .. Awesome ! :)
image.png

Hope this helps someone out there, I'm sure it could save you hours of troubleshooting !

@mcfarhat


Founder of Arab Steem
Arab Steem is a community project to expand Steemit to the Arab world, by supporting the existing Arab steemians and promoting others to join.
You can connect with us on @arabsteem or via discord channel https://discord.gg/g98z2Ya


Proud Member Of

  • steemSTEM: SteemSTEM is a project that aims to increase both the quality as well as visibility of Science, Technology, Engineering and Mathematics (and Health). You can check out some great scientific articles via visiting the project tag #steemSTEM , project page @steemstem, or connecting with us on chat https://steemit.chat/channel/steemSTEM
  • MAP(Minnows Accelerator Project): MAP is a growing community helping talented minnows accelerate their growth on Steemit.
    To join, check out the link at the home page of @accelerator account



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

My husband @bikindrumz is an IT guy I am sure he could relate to this :P

hehe nice!
well only if he is coding things connecting to steemit, then he definitely would.

Hello boss @mcfarhat

Yet again interesting read but nothing understood. I trust you must have dished out rare and quality information in this piss but unfortunately, as always, I am not a code guy.

Upv by
@eurogee, the Steemivangelist and the founder of @euronation Newbie's Support Team

Trying to correct it now. hope it works

Again, my discord app has been misbehaving of recent. I will find a way around that latest today, tomorrow. Thanks

haha thank you @eurogee!

Upvoted and resteemed

Thank you :)

have you tried the get_ops_in_block call? seems api.steemit.com doesn't support this call which keeps return empty result

Thanks for sharing your article on steemit.

Great work @mcfarhat, well planed everything is placed in a very systematic order. Ourstanding work just mervalous ^^

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

What can I us API for?

Hey @mcfarhat I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x