<p>I'm trying to connect to an ElectrumX server from an HTML webpage. But I can't get a working connection.</p>
<p>I've tried with many servers from Electrum's <a href="https://github.com/spesmilo/electrum/blob/cdda1549e91421723c677d751616660db95afb40/electrum/servers.json" rel="nofollow noreferrer"><code>servers.json</code></a> list but none worked.</p>
<p>Here's the JavaScript code that I tried:</p>
<pre><code>const socket = new WebSocket(&quot;ws://electrum.blockstream.info:50002&quot;);

// Connection opened
socket.addEventListener(&quot;open&quot;, () =&gt; {
  console.log(&quot;Connection opened&quot;);
});

socket.addEventListener(&quot;close&quot;, () =&gt; {
  console.log(&quot;Connection closed&quot;);
});

socket.addEventListener(&quot;error&quot;, e =&gt; {
  console.log(&quot;error&quot;, e);
});

socket.addEventListener(&quot;message&quot;, function(event) {
  console.log(&quot;Message from server &quot;, event.data);
});
</code></pre>
<p>I tried both encrypted (wss) and non-encrypted (ws) connection. I tried several servers and different ports. I couldn't get it to connect.</p>
<p>Could someone share a working example of JavaScript code connecting to ElectrumX using WebSockets?</p>

CC BY-SA 4.0
2020-07-27T14:35:33.090Z
If I recall, electrum doesn't use wss (which is really https, right?) it uses raw SSL. So there's no usual header, just raw packets encrypted by SSL
pinhead
CC BY-SA 4.0
2020-07-27T17:56:31.910Z
@pinhead according to the docs, it supports WebSockets, see https://electrumx.readthedocs.io/en/latest/environment.html#services
M.K. Safi
CC BY-SA 4.0
2020-07-28T11:46:03.533Z
Ah you are right, it's the Electrum GUI that is raw SSL only (as of maybe a year ago when I last looked into it)
pinhead
<electrum><javascript><websockets>
How to connect to an ElectrumX server using JavaScript WebSockets?
M.K. Safi
# How to connect to an ElectrumX server using JavaScript WebSockets?

(Asked by: username_0 on 2020-07-27)

I'm trying to connect to an ElectrumX server from an HTML webpage. But I can't get a working connection.
I've tried with many servers from Electrum's servers.json list but none worked.
Here's the JavaScript code that I tried:
const socket = new WebSocket("ws://electrum.blockstream.info:50002");

// Connection opened
socket.addEventListener("open", () => {
  console.log("Connection opened");
});

socket.addEventListener("close", () => {
  console.log("Connection closed");
});

socket.addEventListener("error", e => {
  console.log("error", e);
});

socket.addEventListener("message", function(event) {
  console.log("Message from server ", event.data);
});

I tried both encrypted (wss) and non-encrypted (ws) connection. I tried several servers and different ports. I couldn't get it to connect.
Could someone share a working example of JavaScript code connecting to ElectrumX using WebSockets?

**Tags:** electrum, javascript, websockets

> **Comments:**
> **username_1**: If I recall, electrum doesn't use wss (which is really https, right?) it uses raw SSL. So there's no usual header, just raw packets encrypted by SSL  
> **username_0**: @username_1 according to the docs, it supports WebSockets, see https://electrumx.readthedocs.io/en/latest/environment.html#services  
> **username_1**: Ah you are right, it's the Electrum GUI that is raw SSL only (as of maybe a year ago when I last looked into it)  

---

## 0 answers

