Skip to content

Instantly share code, notes, and snippets.

@itgoldman
Created August 18, 2021 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itgoldman/ef6f90dc1c9cdab8722b1ccbfb4c60bb to your computer and use it in GitHub Desktop.
Save itgoldman/ef6f90dc1c9cdab8722b1ccbfb4c60bb to your computer and use it in GitHub Desktop.
// server serving index.html
var express = require("express");
var path = require("path");
var app = express();
app.get("/", function(req, res) {
res.sendFile(path.join(__dirname, "./index.html"));
});
app.listen(3000);
console.log("Express started on port 3000");
if (true) {
// calling API every 5 seconds or so.
// this is immitating long process.
const https = require("https");
const axios = require("axios");
const agent = new https.Agent({ rejectUnauthorized: false });
var x = 1;
var notify = function() {
// notify client
axios
.post(
"https://rejax.io:3001/api/server",
{
app_key: "KEYzoVONRA4CdMvDzBZXytPyebwMf3sLrLh",
app_secret: "SECRETLJAQpPaB6p31q5UOzOBtsmmnbLqFY6z4",
channel: "my-channel-name",
text: "hello from server at " + new Date()
},
{ httpsAgent: agent }
)
.then(res => {
console.log(res.data);
})
.catch(error => {
console.error(error.code);
});
// do some work
x++;
setTimeout(notify, x * 1000);
};
notify();
}
@itgoldman
Copy link
Author

and this is the client:

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>rejax.io example - client</title>


<!-- Rejax Loader -->
<script>(function (w, d, s, v, p) {
w[v] = w[v] || { listen: function (a, b) { this.l[a] = b }, l: {} };
w[v].app_key = p; var f = d.getElementsByTagName(s)[0], j = d.createElement(s);
j.async = true; j.src = 'https://rejax.io:3001/api/client'; f.parentNode.insertBefore(j, f);
})(this, document, 'script', 'Rejax', 'KEYzoVONRA4CdMvDzBZXytPyebwMf3sLrLh')</script>
<!-- End Rejax Loader -->
</head>

<body>
	<h1>rejax.io example - client</h1>
  <p>
    just make sure app key and app secret and domain (client origin) are configured in rejax.io
  </p>
	<ul>
		<li>open developers console</li>
		<li>watch incoming messages</li>
	</ul>
  
  wait for it...
	<pre id="incoming"></pre>

	<script>
		Rejax.listen('my-channel-name', function (text) {

			// received text from server
			console.log(text)

			document.getElementById('incoming').innerText += text + '\n'
		})
	</script>


</body>

</html>```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment