Skip to content

Instantly share code, notes, and snippets.

@dheerajsuthar
Created December 6, 2018 21:19
Show Gist options
  • Save dheerajsuthar/deeb2e32a1eebd67fe325aebf9ba2faa to your computer and use it in GitHub Desktop.
Save dheerajsuthar/deeb2e32a1eebd67fe325aebf9ba2faa to your computer and use it in GitHub Desktop.
Simple Publisher using Client WebSockets API
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Tahoma, Geneva, sans-serif;
}
div {
display: inline;
}
</style>
<script>
function publish() {
var message = document.getElementById('message').value;
var channel = document.getElementById('channel').value;
var host = window.document.location.host.replace(/:.*/, '');
var ws = new WebSocket('ws://' + host + ':8080');
ws.onopen = function () {
ws.send(JSON.stringify({
request: 'PUBLISH',
message: message,
channel: channel
}));
ws.close();
};
}
</script>
</head>
<body>
<h1>Publisher</h1>
<input type="text" id="channel" placeholder="Channel (weather, sports etc.)" />
<input type="text" id="message" placeholder="What you want to publish?" />
<button onclick="publish()">Publish</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment