isaurssaurav / walkie-talkie Goto Github PK
View Code? Open in Web Editor NEWHow to make a browser walkie-talkie using Node.JS and Socket.IO
How to make a browser walkie-talkie using Node.JS and Socket.IO
Hi Saurav,
I enjoyed your 2018 article, thanks.
No way to run it using HTTP. I guess the issue is here:
navigator.mediaDevices is undefined in an insecure context.
See: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
Problem solved using an HTTPS server. To thank you, here the running code, that could substitute the original index.js
:
/*
* @see https://stackoverflow.com/questions/31156884/how-to-use-https-on-node-js-using-express-socket-io
*
* $ sudo ufw allow 8443
* $ sudo ufw status verbose
*
*/
const fs = require('fs')
const https = require('https')
const express = require('express')
const app = express()
//
// HTTPS server setup
//
// configurations
const PORT = 8443
const KEY_FILENAME = 'selfsigned.key'
const CERT_FILENAME = 'selfsigned.cert'
//
// get SSL certificate
//
const credentials = {
key: fs.readFileSync(KEY_FILENAME, 'utf8'),
cert: fs.readFileSync(CERT_FILENAME, 'utf8')
}
// create the https server and the socketio channel
const server = https.createServer(credentials, app)
const io = require('socket.io')(server)
// root route
app.get('/', (req, res) =>
res.sendFile(__dirname + '/index.html'))
// run the https server
server.listen(PORT, () => {
console.log('server running.')
console.log(`Listening on port: ${PORT}`)
console.log()
})
//
// socketio messaging application
//
// applicaction variables
let Usercounter = 0
//
// connection/disconnection logic
//
io.on('connection', (socket) => {
Usercounter = Usercounter + 1
io.emit('user', Usercounter)
console.log('a user is connected')
socket.on('disconnect', () => {
Usercounter = Usercounter - 1
io.emit('user', Usercounter)
console.log('user disconnected')
})
//
// echo server logic:
// the audio message received is returned back
//
socket.on('audioMessage', (msg) => {
io.emit('audioMessage', msg)
})
})
respect
giorgio
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.