Skip to content

Commit

Permalink
Update the Minimal template with the new SDK changes (#133)
Browse files Browse the repository at this point in the history
* Update the Minimal template with the new SDK changes

* allow user to set the token through console
  • Loading branch information
iAmmar7 authored Feb 28, 2025
1 parent e8d55c2 commit f6b7567
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ app.get('/', async (req, res) => {
app.get('/minimal', async (req, res) => {
let token
if (req.session && req.session.token) {
token = session.token
token = req.session.token
}

res.render('minimal', {
Expand Down
25 changes: 14 additions & 11 deletions public/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,20 @@ function restoreUI() {
}

async function initSWClient() {
if (!client && _token) {
client = await SWire({
host: !!_host && _host.trim().length ? _host : undefined,
token: _token,
debug: {
logWsTraffic: true,
},
logLevel: 'debug',
})
if (!_token) {
console.error('Token not found')
return
}

client = await SWire({
host: !!_host && _host.trim().length ? _host : undefined,
token: _token,
debug: {
logWsTraffic: true,
},
logLevel: 'debug',
})

return client
}

Expand All @@ -429,8 +432,8 @@ sendMessageBtn.addEventListener('click', async () => {
* Connect with Relay creating a client and attaching all the event handler.
*/
window.dial = async () => {
if (!_token) {
console.error('Auth required!')
if (!client) {
console.error('Client is not initialized!')
return
}

Expand Down
44 changes: 35 additions & 9 deletions public/minimal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { SignalWire: SWire } = SignalWire

let client = null
let pnSecretKey = null

Expand All @@ -15,28 +17,52 @@ window.ready = (callback) => {
}
}

async function connect() {
if (!_token) return

client = await SignalWire.SignalWire({
host: _host,
async function initSWClient() {
if (!_token) {
console.error('Token not found')
return
}

client = await SWire({
host: !!_host && _host.trim().length ? _host : undefined,
token: _token,
rootElement: document.getElementById('rootElement'),
debug: {
logWsTraffic: true,
},
logLevel: 'debug',
})

await client.connect()
return client
}

async function makeCall() {
async function connect() {
// Set the token through the console
_token = window._token
initSWClient()
}

async function dial() {
if (!client) {
console.error('Client is not initialized!')
return
}

const call = await client.dial({
to: document.getElementById('destination').value,
logLevel: 'debug',
debug: { logWsTraffic: true },
rootElement: document.getElementById('rootElement'),
})

window.__call = call

await call.start()
}

async function hangup() {
await window.__call.leave()
}

async function enablePushNotifications() {
btnRegister.disabled = true

Expand Down Expand Up @@ -171,7 +197,7 @@ async function readPushNotification(payload) {
}

window.ready(async function () {
await connect()
await initSWClient()

const searchParams = new URLSearchParams(location.search)
console.log('Handle inbound?', searchParams.has('inbound'))
Expand Down
8 changes: 5 additions & 3 deletions views/minimal.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
<div class="container">
<div class="row"></div>
<div class="col-3">
<button class="btn btn-primary my-2" onclick="connect();">Connect</button>

<label for="destination" class="form-label">Destination</label>
<input type="text" id="destination" class="form-control" value="<%= destination %>">
<button class="btn btn-primary" onclick="makeCall();">Call</button>
<button class="btn btn-primary my-1" onclick="dial();">Call</button>
<div class="card mt-2">
<div class="d-grid gap-2">
<a id="btnRegister" class="btn btn-success" href="/minimal?inbound">
Expand Down Expand Up @@ -64,13 +66,13 @@
}
</script>
<script type="text/javascript">
const _token = "<%= token %>";
let _token = "<%= token %>";
const _host = "<%= host %>";
const _fabricApiUrl = "<%= fabricApiUrl %>";
const _firebaseConfig = <%- firebaseConfig %>;
</script>
<script type="text/javascript" src="https://unpkg.com/@signalwire/js@dev"></script>
<script type="text/javascript" src="/minimal.js"></script>
<script type="text/javascript" src="minimal.js"></script>
</body>

</html>

0 comments on commit f6b7567

Please sign in to comment.