Live streaming website with real-time chatting function.
You can give it a try with this link!
The SSL certificate is free, so it has only three months of life.
In the home page, users can choose a chatting room they want to join.
For example, user A sets the name like Tony, and room name as ABC. If room ABC doesn't exist, the server will create it automatically. Here comes user B named Tina, she sets the room name as ABC, which is created by Tony previously, then Tina will join and see Tony in the room.
By using socket.io
and peerjs
, they can chat through real-time messages or streaming video.
Windows 10 64-bit 20H2, nodejs v12.18.4 Ubuntu 18.04, nodejs v12.19.1 Ubuntu 18.04, Docker 19.03.6
I used tree /f
command to print out directory paths and files and made it more concise. I filtered material kit
out of the tree structure below because that's not the point for this repo. The basic structure is setup by running express --view=ejs --css=sass
, if you're interested in it, the instruction is here.
WebRTC-Live-Streaming:.
│ .gitignore
│ app.js
│ package-lock.json
│ package.json
│ Procfile
│ README.md
│
├─bin
│ www
│
├─public
│ ├─demo
│ ├─images
│ ├─javascripts
│ │ main.js
│ │
│ └─stylesheets
│ style.css
| style_chat.css
|
├─routes
│ index.js
│ users.js
│
├─utils
│ messages.js
│ users.js
│
└─views
chat.ejs
index.ejs
-
Back-end
-
./bin/www
:The entry of the application generated by the Express Application Generator. The first thing it does is to require('../app') which is
app.js
in the root, the “real” entry point. Besides, it runs all the backend server funcions, includingsocket.io
realtime chat messages and basicExpress
function. -
app.js
:Generated by the Express Application Generator.
I didn't modify it.
-
utils\messages.js
:Format the chat message with user name, text and time. Called by
./bin/www
. -
utils\users.js
:Handles members info in the room, such as user who joining and leaving the room, current user and room members. Called by
./bin/www
. -
routes\index.js
:The route file that loads the express module and the uses it to get an
express.Router
object. Then it specifies a route on that object and lastly exports the router from the module. I add some code to render the chatting room(thechat.ejs
) and route the user to it.router.get('/chat', function(req, res, next) { res.render('chat', { title: 'WebRTC-Live-Steaming', AddressBar: AddressBar, navbar: navbar, url: url, }); });
-
-
Front-end
-
views\index.ejs
:The entry page. Users can choose a chatting room they want to join in the index page.
-
views\chat.ejs
Calls the
socket.io.js
,peerjs
andmain.js
, users can chat and also see each other in the chat page. -
public\stylesheets\style_chat.css
andstyle.css
:The temporary css file which renders the page. I will render the page with material kit in the next version.
-
public\javascripts\main.js
:The most important part of the frontend page, it deals chatting messages, media settings, and peer to peer communication.
-
-
Clone the repository and open it.
-
Install all the packages that you need.
$ npm install
-
Install
nodemon
.$ npm install --save-dev nodemon
If you want to use this application via IP address or domain name instead of localhost
, this step is necessary. Since most of browsers block media(such as webcam) accessing from unsecure connections, we have to deploy this application with HTTPS
. Here are commands using openssl
to generate a key and certificate.
# generate a key
$ openssl genrsa -out server-key.pem
# this step will probably ask you to input the information of the signature,
# such as country, company name, etc
$ openssl req -new -key server-key.pem -out csr.pem
# generate a certificate
$ openssl x509 -req -days 9999 -in csr.pem -signkey server-key.pem -out server-cert.pem
# optional: we dont need this, but I think it's okay
$ rm csr.pem
-
Run the server
$ npm run serverstart
-
Access the website
https://Your_IP_Address
- Install docker if you don't have it.
sudo apt update sudo apt install docker.io # Start docker service systemctl enable docker systemctl start docker # Verify: you should see no error messages systemctl status docker sudo docker run hello-word
- Build docker image
sudo docker build -t webrtc:latest . --no-cache --rm # Verify: you should see a image named "webrtc" sudo docker images
- Run the image as a container
# -it: Create an interactive bash # -p: Publish a container’s port(s) to the host # -d: Run container in background sudo docker run -it -p 443:443 -p 3001:3001 -p 80:80 -d --restart=always --name=webrtc_app webrtc:latest # Verify: you should see container statu "Up seconds" sudo docker ps
- Play around
- Container
# Show all the containers sudo docker ps # Action to the container sudo docker stop/start/restart webrtc_app # Remove the container sudo docker rm webrtc_app # Display stdout of the container sudo docker logs webrtc_app # Login to bash of the container sudo docker exec -it webrtc_app /bin/bash
- Image
# Show all the images sudo docker images # Delete the image sudo docker rmi webrtc # Clean unused images sudo docker images prune
- Container