ably / ably-js

Javascript, Node, Typescript, React, React Native client library SDK for Ably realtime messaging service
https://ably.com/download
Apache License 2.0
310 stars 55 forks source link

Error "No key specified" since ably update (1.2.3 > 2.3.0) #1822

Open DanielMoindrot opened 1 month ago

DanielMoindrot commented 1 month ago

Hi,

I'd copied the realtime-quiz-framework on an ubuntu vps. I did some tests and that's worked very well.

But, after I'd updated all dependecies it doesn't work anymore. Now, once the server is launched, as soon as we connect to it I have the error

root@ubuntu:~/realtime-quiz-framework# node server.js {

parsed: { ABLY_API_KEY: 'rWiZlg.apsiJQ:somekey', PORT: '443' } } rWiZlg.apsiJQ:somekey Your app is listening on port 443 /root/realtime-quiz-framework/node_modules/ably/build/ably-node.js:2060 throw new ErrorInfo("No key specified", 40101, 403); ^

_ErrorInfo: No key specified at Auth.createTokenRequest (/root/realtime-quiz-framework/node_modules/ably/build/ably-node.js:2060:13) at /root/realtime-quiz-framework/server.js:34:17 at Layer.handle [as handle_request] (/root/realtime-quiz-framework/node_modules/express/lib/router/layer.js:95:5) at next (/root/realtime-quiz-framework/node_modules/express/lib/router/route.js:149:13) at Route.dispatch (/root/realtime-quiz-framework/node_modules/express/lib/router/route.js:119:3) at Layer.handle [as handle_request] (/root/realtime-quiz-framework/node_modules/express/lib/router/layer.js:95:5) at /root/realtime-quiz-framework/node_modules/express/lib/router/index.js:284:15 at Function.process_params (/root/realtime-quiz-framework/node_modules/express/lib/router/index.js:346:12) at next (/root/realtime-quiz-framework/node_modules/express/lib/router/index.js:280:10) at SendStream.error (/root/realtime-quiz-framework/node_modules/serve-static/index.js:121:7) { code: 40101, statusCode: 403, cause: undefined }

Node.js v18.19.1

"dependencies": { "ably": "^2.3.0", "dotenv": "^16.4.5", "express": "^4.19.2", "path": "^0.12.7", "serve-static": "^1.15.0" }, "devDependencies": { "babel-eslint": "^10.1.0", "eslint": "^9.8.0", "eslint-plugin-vue": "^9.27.0" } By the way i can go back but i need some help to understand this issue.

┆Issue is synchronized with this Jira Task by Unito

VeskeR commented 1 month ago

Hi @DanielMoindrot ! Thank you for bringing this up.

realtime-quiz-framework code has not been updated to correctly use ably-js v2 yet. In this particular case you're seeing this error because Auth.createTokenRequest method that server.js file is calling here has been changed in v2 to not accept a callback anymore, instead it returns a promise. When server.js passes that callback as a second argument, the library expects that argument to be a authOptions object containing the key which overrides the default one set via the client options.

To fix this issue you can change the app.get('/auth', (request, response) => { call as follows:

app.get('/auth', async (request, response) => {
  const tokenParams = { clientId: uniqueId() };
  try {
    const tokenRequest = await realtime.auth.createTokenRequest(tokenParams);
    response.setHeader('Content-Type', 'application/json');
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.send(JSON.stringify(tokenRequest));
  } catch (error) {
    response
      .status(500)
      .send('Error requesting token: ' + JSON.stringify(error));
  }
});

Note that you also need to change import statement const Ably = require('ably/promises'); to const Ably = require('ably'); in quiz-room-server.js file here.

I was able to make it work locally after those two changes, but there are still might be some slight changes required after upgrading to v2 (I didn't test that thoroughly). You can follow the ably-js v2 migration guide to see what was affected.