Skip to content

Backend

Course: WebRTC Basic implementation, advance discussion - chapter 5

  • Create an empty TS project, add ws to it
Terminal window
npm init -y
npx tsc --init
npm install ws @types/ws
  • Change rootDir and outDir in tsconfig
Terminal window
"rootDir": "./src",
"outDir": "./dist",
  • Create a simple websocket server
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
let senderSocket: null | WebSocket = null;
let receiverSocket: null | WebSocket = null;
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
ws.on('message', function message(data: any) {
const message = JSON.parse(data);
});
ws.send('something');
});
  • Try running the server
tsc -b
node dist/index.js
  • Add message handlers
$1b

That is all that you need for a simple one way communication b/w two tabs

To have both the sides be able to send each other media, and support multiple rooms, see https://github.com/hkirat/omegle/