To set up a basic relay, initialize rtsp-relay by passing your Express app instance. Use the proxy function to create a handler for a websocket route. The proxy function accepts an options object containing the url of the RTSP stream.
On the client side, use the scriptUrl provided by the module to load the player, then call loadPlayer with the websocket URL and a target <canvas> element.
const express = require('express');
const app = express();
const { proxy, scriptUrl } = require('rtsp-relay')(app);
const handler = proxy({
url: `rtsp://admin:admin@10.0.1.2:554/feed`,
verbose: false,
});
// The endpoint our RTSP uses
app.ws('/api/stream', handler);
// Example HTML page to view the stream
app.get('/', (req, res) =>
res.send(`
<canvas id='canvas'></canvas>
<script src='${scriptUrl}'></script>
<script>
loadPlayer({
url: 'ws://' + location.host + '/api/stream',
canvas: document.getElementById('canvas')
});
</script>
`),
);
app.listen(2000);
const express = require('express');
const app = express();
const { proxy, scriptUrl } = require('rtsp-relay')(app);
const handler = proxy({
url: `rtsp://admin:admin@10.0.1.2:554/feed`,
verbose: false,
});
// the endpoint our RTSP uses
app.ws('/api/stream', handler);
// this is an example html page to view the stream
app.get('/', (req, res) =>
res.send(`
<canvas id='canvas'></canvas>
<script src='${scriptUrl}'></script>
<script>
loadPlayer({
url: 'ws://' + location.host + '/api/stream',
canvas: document.getElementById('canvas')
});
</script>
`),
);
app.listen(2000);