How to secure a web socket connection #2824
Replies: 2 comments
-
If you use // client
const socketInstance = io(apiUrl, {
transports: ["polling", "websocket"], // TODO: check websocket, as it doesn't support additional headers
extraHeaders: {
Authorization: `Bearer ${token}`
}
}); and then: // server
io = new Server(server, {
path: "/socket.io",
transports: ["polling", "websocket"]
});
io.engine.use(ClerkExpressRequireAuth()); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am using nextjs 14 with
ws
andtrpc
to establish a web sockets connection.My goal would be the secure the web socket connection with clerk.
As of now, the web socket connection is not secured, while the regular trpc requests are (in trpc configured with
splitLink
).It makes sense that the nextjs middleware does not apply to the web socket connection, as I do start a separate server for this.
I also know that you can secure
ws
calls upon theserver.upgrade
event: https://github.com/websockets/ws?tab=readme-ov-file#client-authenticationI was not able to find a straight forward way to secure the endpoint and the the current user.
It is possible, as I could even do the validation manually: https://clerk.com/docs/backend-requests/handling/manual-jwt
But, I would like to avoid that.
So, I try to find a way to secure it with
@clerk/backend
and maybe also@clerk/clerk-sdk-node
.But I have not been able to really find a way to apply the exported functions.
Here is my minimal repo with the unsecured web socket connection and clerk setup: https://github.com/NicoVogel/t3-websocket-minimal/tree/feat/clerk
Has someone done a more manual approach to setup clerk and may have an idea how to handle this or am I completely on the wrong track?
Beta Was this translation helpful? Give feedback.
All reactions