I am following this tutorial
I am a little lost with getting Ably working for realtime connections.
Everything compiles without error but I start getting 404's in console for the /publish and /subscribe URL's. I get that it is failing on the code in my server.js file (I assume in the realtime auth createTokenRequest), which is as follows:
const app = express();
app.use(cors());
const realtime = Ably.Realtime({
key: process.env.ABLY_API_KEY,
});
app.use(express.static(path.join(__dirname, 'testext/build')));
app.get("/publish", (request, response) => {
const tokenParams = {
capability: '{"*":["publish"]}',
};
realTimeAuth(tokenParams, response);
});
app.get("/subscribe", (request, response) => {
const tokenParams = {
capability: '{"*":["subscribe"]}',
};
realTimeAuth(tokenParams, response);
});
const realTimeAuth = (tokenParams, response) => {
realtime.auth.createTokenRequest(tokenParams, function (err, tokenRequest) {
if (err) {
response
.status(500)
.send("Error requesting token: " + JSON.stringify(err));
} else {
// return the token request to the front-end client
response.json(tokenRequest);
}
});
};
const listener = app.listen(process.env.PORT, () => {
console.log("App is listening on port " + listener.address().port);
});
Any help on why the error is happening would be much appreciated.