-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ably: Auth.requestToken(): token request signing call returned error; err = Error: input
must not start with a slash when using prefixUrl
#1562
Comments
➤ Automation for Jira commented: The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-4014 |
Hey @TranquilMarmot, thanks for reaching out!
I think there's a bit of ambiguity here, typically we refer to the realtime SDK instance as the client, but if you're also using a REST SDK instance to generate signed token requests then this is also a client. In your case, only the REST client has access to your Ably API key, and you're absolutely right that this client should only be created on the server side. On the other hand, the realtime client creates a stateful connection in order to subscribe to realtime updates so if you want your client web application to receive realtime messages over Ably this realtime client must be created on the client side. If you are using a token auth method (such as The " In my experience, NextJS is quite eager to pre-render JSX into HTML on the server side for the sake of optimisation so I'm fairly certain what's happening here is that your NextJS server is running the In order to get rid of the error I would recommend adding a |
Thank you for the detailed response! I think you're right that it's because the server is rendering the The comment that I linked above (ably-labs/react-hooks#8 (comment)) makes a lot more sense now 😅
Just adding "use client";
import dynamic from "next/dynamic";
const DynamicAblyProvider = dynamic(
() => import("./my-ably-provider"),
{
ssr: false, // this ensures that server side rendering is never used for this component
},
);
export default DynamicAblyProvider; This is a bit of a bummer! I wonder if there could be a check in the JS client to detect that it's running outside of a browser context and then skip doing the auth call with I wonder if this would be an issue with server rendering in the example in the Ably tutorials; https://ably.com/tutorials/how-to-ably-react-hooks#tutorial-step-3 |
Another workaround that's a little less crazy... you can wrap creating the client inside of a const createAblyRealtimeClient = (authParams?: Record<string, string>) =>
new Realtime({
authUrl: AUTH_URL,
authMethod: "POST",
authParams: authParams ?? {},
useTokenAuth: true,
});
export const useAblyRealtimeClient = (authParams?: AblyAuthParams) => {
const clientRef = useRef<Realtime | null>(null);
useEffect(() => {
if (typeof window !== "undefined") {
clientRef.current?.close();
clientRef.current = createAblyRealtimeClient(authParams);
}
return () => {
clientRef.current?.close();
clientRef.current = null;
};
}, []);
return clientRef.current;
}; When using it, you can manually do a fallback... export const MyAblyProvider: FunctionComponent<
PropsWithChildren<AblyProviderProps>
> = ({ session, children }) => {
const client = useAblyRealtimeClient();
// this means we're rendering on the server
if (!client) {
return <div>Loading...</div>
}
if (!session) {
return children;
}
return (
<AblyProvider client={client} id="some-id">
{children}
</AblyProvider>
);
}; |
I'm trying to add Ably to a Next.js 14 project.
On the client-side, I'm creating the Ably client like this:
I'm using the
AblyProvider
like so:And then on the server, I have a file at
(...)/api/ably-auth/route.ts
that's doing this:This seems to be working just fine. The server responds with a
tokenRequest
.But, in the server logs, every time that
createTokenRequest
is called, this is printed out:But this doesn't make any sense to me:
If I'm doing token authentication, wouldn't I ONLY ever want to create a client on the server? Otherwise, I'd be passing the API key to the client which seems like a Bad Idea.
Maybe I'm missing something here? Are we not supposed to use the JS client on the server?
These docs seem to suggest that this is what we're supposed to do:
https://ably.com/docs/auth/token?lang=javascript#token-request
┆Issue is synchronized with this Jira Story by Unito
The text was updated successfully, but these errors were encountered: