Skip to content

Conversation

@carolcarriazo
Copy link
Contributor

No description provided.

@carolcarriazo
Copy link
Contributor Author

@splindsay-92 Hey Steven... managed to commit myself :) This works with Netlify, but my code etiquette may be a bit crap. Also still working on the readme. would be great if you could review in the meanwhile


export default function Chat() {
const realtimeClient = new Ably.Realtime({ authUrl: '/api' });
const realtimeClient = new Ably.Realtime({ authUrl: 'http://localhost:8888/.netlify/functions/auth' }); // make sure this matches your localhost server
Copy link
Contributor Author

@carolcarriazo carolcarriazo May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to get this to work with a relative path. open to suggestions @splindsay-92 .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the error it was giving you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I set authUrl to .netlify/functions/auth:

 Ably: Auth.requestToken(): token request signing call returned error; err = TypeError: Invalid URL
 GET / 200 in 1298ms
Request from ::1: GET /.netlify/functions/auth?rnd=9905685111520733
Response with status 200 in 65 ms.

if I set authUrl to /.netlify/functions/auth:

13:01:40.149 Ably: Auth.requestToken(): token request signing call returned error; err = Error: `input` must not start with a slash when using `prefixUrl`
 GET / 200 in 1338ms
Request from ::1: GET /.netlify/functions/auth?rnd=1762475061762131
Response with status 200 in 44 ms.

if I set authUrl to ${window.location.origin}/.netlify/functions/auth (to try to bypass prefixUrl error), I get the same error as above.

fixing chat import
@carolcarriazo
Copy link
Contributor Author

draft of the actual article is here: https://ghost.ably.com/ghost/#/editor/post/681b53d118da650028141ff2

removing room reactions line
small fixes
fixing sendMessage
make pretty
@@ -0,0 +1,6 @@
[build]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there are no redirects here, probably why your API call didn't work correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also the adapter plugin, but can't see this in your packages file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry what adapter plugin?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one netlify/plugin-nextjs :)

});

const sendChatMessage = async (text) => {
if (!sendMessage) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always be truthy, send is actually a promise, so the check can be removed.

const client = new Ably.Rest(process.env.ABLY_API_KEY);
const tokenRequest = await client.auth.createTokenRequest({
clientId: "ably-chat-demo",
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're also missing the capability definitions here

const tokenRequestData = await client.auth.createTokenRequest({
    capability: {
      '[chat]*': ['*'],
      '*': ['*'],
    },
    clientId: clientId,
  });

Also, the clientId should really be passed to this handler in the GET call and not defined here. The above will work, but its not really correct and would be an anti-pattern.

splindsay-92 and others added 2 commits May 16, 2025 15:20
- Major overhaul to use ts.
- Correctly redirect to auth function, it now accepts a clientId and adds chat capabilities
- Added tool version file with nodejs dep.
- Added next.ts config.
- Updated package.json
- Updated eslint
- Updated README.md
- Randomly generate clientIds on render
…l-overhaul

Refactor: Refactor to use ts and netlify functions
@carolcarriazo carolcarriazo merged commit b6253e8 into main May 19, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants