Last active
May 20, 2022 05:29
-
-
Save thameera/eda855fdc0bd1a1289076b726c25edbe to your computer and use it in GitHub Desktop.
Socketio-jwt example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Socket.IO chat</title> | |
<style> | |
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; } | |
#form { background: rgba(0, 0, 0, 0.15); padding: 0.25rem; position: fixed; bottom: 0; left: 0; right: 0; display: flex; height: 3rem; box-sizing: border-box; backdrop-filter: blur(10px); } | |
#input { border: none; padding: 0 1rem; flex-grow: 1; border-radius: 2rem; margin: 0.25rem; } | |
#input:focus { outline: none; } | |
#form > button { background: #333; border: none; padding: 0 1rem; margin: 0.25rem; border-radius: 3px; outline: none; color: #fff; } | |
#messages { list-style-type: none; margin: 0; padding: 0; } | |
#messages > li { padding: 0.5rem 1rem; } | |
#messages > li:nth-child(odd) { background: #efefef; } | |
</style> | |
</head> | |
<body> | |
<ul id="messages"></ul> | |
<form id="form" action=""> | |
<input id="input" autocomplete="off" /><button>Send</button> | |
</form> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
const TOKEN = 'PASTE_TOKEN_HERE' | |
var socket = io('http://localhost:3000', { | |
auth: { | |
token: `Bearer ${TOKEN}` | |
} | |
}); | |
socket.on('connect_error', (error) => { | |
console.log(error) | |
}) | |
var form = document.getElementById('form'); | |
var input = document.getElementById('input'); | |
form.addEventListener('submit', function (e) { | |
e.preventDefault(); | |
if (input.value) { | |
socket.emit('chat message', input.value); | |
input.value = ''; | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
const app = express(); | |
import http from 'http' | |
const server = http.createServer(app); | |
import { Server } from 'socket.io' | |
import { authorize } from '@thream/socketio-jwt' | |
import jwksClient from 'jwks-rsa' | |
import path from 'path'; | |
const __dirname = path.resolve(); | |
const client = jwksClient({ | |
jwksUri: 'https://tham.auth0.com/.well-known/jwks.json' | |
}) | |
const io = new Server(server); | |
io.use( | |
authorize({ | |
algorithms: ['RS256'], | |
secret: async (decodedToken) => { | |
const key = await client.getSigningKey(decodedToken.header.kid) | |
return key.getPublicKey() | |
} | |
}) | |
) | |
io.on('connection', (socket) => { | |
console.log('a user connected'); | |
socket.on('chat message', (msg) => { | |
console.log('message: ' + msg); | |
}); | |
socket.on('disconnect', () => { | |
console.log('user disconnected'); | |
}); | |
}); | |
app.get('/', (req, res) => { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
server.listen(3000, () => { | |
console.log('listening on *:3000'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "socket-io", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"type": "module", | |
"scripts": { | |
"start": "nodemon --experimental-modules index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "Thameera Senanayaka <[email protected]> (https://thameera.com/)", | |
"license": "MIT", | |
"dependencies": { | |
"@thream/socketio-jwt": "^3.0.0", | |
"express": "^4.18.1", | |
"jwks-rsa": "^2.1.2", | |
"socket.io": "^4.5.1" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Uses https://www.npmjs.com/package/@thream/socketio-jwt | |
Template based on the chat example at: https://socket.io/get-started/chat | |
Run with: | |
``` | |
node index.js | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment