I am using the new, free Back4App.com Containers to set up a PeerJS Server. I have detailed how to do that below.
You are given a url, eg "https://mypeerjsserver10-XXXXX.b4a.run". The XXXXX are my user name.
This url gives the correct response:
{"name":"PeerJS Server","description":"A server side element to broker connections between PeerJS clients.","website":"https://peerjs.com/"}
How do I connect to this Back4App server from my javascript client code?
I thought it would be:
const peer = new Peer(id, {host: "mypeerjsserver10-XXXXX.b4a.run", port: 443, secure: true});
But this does not connect.
Is it the host
, port
or secure
? Do I need a path
? If so, what path
?
If you want to try this, it is simple to set up.
Setting Up Your Own Github PeerJS Server Repository
There is a slight issue with the newest version of PeerJS Server today (16th Mar 2023), so you will need to use the stable version.
So set up an empty Github project with just a Dockerfile with the single line contents:
FROM peerjs/peerjs-server:1.0.0
Setting Up Your Back4App Container
Back4App has made this very simple as they process this with the Dockerfile in your Github repository.
- On the 'My Apps' page when signed into Back4App.com, click 'Build New App'.
- Click the 'Containers as a Service'.
- You will need to connect your Github account (must be the same email). Once you do, your Github projects will appear. Then click 'Select' for your copy of PeerJS Server in your Github account.
- Fill out the form, eg App Name: mypeerjsserver, then the rest as defaults.
- Click 'Create App'
When it loads everything, in the top left you will get a green tick and the word 'Available'. Underneath will be your url for the app, eg mypeerjsserver10-XXXXX.b4a.run
.
If you click on that url you will direct to a webpage with the following:
{"name":"PeerJS Server","description":"A server side element to broker connections between PeerJS clients.","website":"https://peerjs.com/"}
So it is all working.
Note the url is an https
so it is secure.
Testing the PeerJS Container
There is a Jsfiddle by someone else.
https://jsfiddle.net/NathanFriend/q6tch0az/39/
He uses the general server run by the PeerJS creators.
He connects to that with the line:
const peer = new Peer(id);
To connect it to your server, you have to edit this line to something like:
const peer = new Peer(id, {host: "mypeerjsserver10-XXXXX.b4a.run", port: 443, secure: true});
But this obviously does not work.