Skip to content

Instantly share code, notes, and snippets.

@chr15m
Created September 26, 2025 01:22
Show Gist options
  • Save chr15m/265f37a2bbd544de9e89083923474a04 to your computer and use it in GitHub Desktop.
Save chr15m/265f37a2bbd544de9e89083923474a04 to your computer and use it in GitHub Desktop.
Test Nostr WebSocket connection from file:/// static HTML
<!DOCTYPE html>
<html>
<head>
<title>Nostr Feed Test</title>
<style>
body {
background-color: #222;
color: #eee;
font-family: 'Courier New', Courier, monospace;
}
#feed {
max-width: 800px;
margin: 0 auto;
}
.note {
padding: 10px;
border-bottom: 1px solid #555;
}
</style>
</head>
<body>
<div id="feed"><h1>Nostr WebSocket Test</h1></div>
<script>
const relay = "wss://nos.lol";
const npub = "da93192957495fb59f6ef1ce19e74947b0792b6eaa1b134a015c0326e4097d1a";
const feedDiv = document.getElementById('feed');
const ws = new WebSocket(relay);
ws.onopen = () => {
console.log("socket open, sending sub.");
const sub = ["REQ", "my-sub", { "authors": [npub] }];
ws.send(JSON.stringify(sub));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data[0] === "EVENT" && data[2]) {
const note = data[2];
const noteDiv = document.createElement('div');
noteDiv.className = 'note';
const date = new Date(note.created_at * 1000).toLocaleString();
noteDiv.textContent = `${date}: ${note.content}`;
feedDiv.appendChild(noteDiv);
} else if (data[0] === "EOSE") {
ws.close();
}
};
ws.onclose = () => console.log("socket closed.");
ws.onerror = (error) => console.error("WebSocket Error:", error);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment