Skip to content

Instantly share code, notes, and snippets.

@arisetyo
Created May 24, 2021 02:47
Show Gist options
  • Save arisetyo/bd04f8ca0411cbb4f9341e1ac5406abd to your computer and use it in GitHub Desktop.
Save arisetyo/bd04f8ca0411cbb4f9341e1ac5406abd to your computer and use it in GitHub Desktop.
Beams SDK integration for push notification on the browser
<!DOCTYPE html>
<html lang="en">
<head>
<title>Beams SDK integration</title>
</head>
<body>
<h1>Beams SDK integration</h1>
<!-- Install the SDK -->
<script src="https://js.pusher.com/beams/1.0/push-notifications-cdn.js"></script>
<!-- Register your first web device -->
<script>
// A. Initialize the SDK
// use the instance ID provided in the Beams' instance dashboard
const beamsClient = new PusherPushNotifications.Client({
instanceId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
});
// B. Subscribe to the Device Interest `hello`
// With Device Interests, a device “subscribes” to an Interest.
beamsClient.start()
.then(() => beamsClient.addDeviceInterest('hello'))
.then(() => console.log('Successfully registered and subscribed!'))
.catch(console.error);
</script>
</body>
</html>
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
app.use(bodyParser.json());
// load static file
app.use(express.static(path.join(__dirname, './')));
// run the Express server on port 3000
app.set('port', 3000);
const server = app.listen(app.get('port'), () => {
console.log(`Express running on PORT ${server.address().port}`);
});
// Create a service worker
importScripts("https://js.pusher.com/beams/service-worker.js");
// Serve this from http://localhost:3000/service-worker.js
// Open http://localhost:3000/service-worker.js in your browser to verify that it is being served. You should see the plaintext content of the file you just created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment