Join and earn Bitcoin Cash for participation

electrum-cash: connecting to a server

1 111 exc boost
Avatar for JonathanSilverblood
Written by   134
3 months ago (Last updated: 6 days ago)

Introduction

Electrum-cash is a javascript/typescript library for communicating with electrum servers.

This example demonstrates how to create a connection to a server and how to cleanly shut down when you're done using it.

Including the client library in your project

When you are using a single server for your project, you should only include the client, either in CommonJS or as an ES2015 module depending on your project.

// Import the electrum client in CommonJS.
const { ElectrumClient } = require('electrum-cash');
// Import the electrum client as an ES2015 module.
import ElectrumClient from 'electrum-cash';

Set up the library and connect with a server

You will need to create an instance of the electrum client with the server connection electrum clientdetails for the server you want to use.

// Initialize an electrum client.
const electrum = new ElectrumClient('Your Electrum App Name', '1.4.3', 'bch.imaginary.cash');

Waiting for the connection to be established

Before you can start using the client, you need to connect to the server and wait for it to complete version negotiation.

// Wait for the client to connect and complete version negotiation.
await electrum.connect();

Shutting down when you're done

After you're done with the server, you should disconnect from it to shutdown gracefully.

// Close the connection.
await electrum.disconnect();

32
$ 1.23
$ 0.50 from @emergent_reasons
$ 0.28 from @TheRandomRewarder
$ 0.25 from @rosco
+ 2
Avatar for JonathanSilverblood
Written by   134
3 months ago (Last updated: 6 days ago)
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.

Comments