-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b479d0c
commit 9e48099
Showing
11 changed files
with
213 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use client"; | ||
|
||
import { createCable } from "@anycable/web"; | ||
import { createContext, useContext } from "react"; | ||
|
||
const getAuthenticatedCable = (token: string) => | ||
createCable( | ||
`${process.env.NEXT_PUBLIC_ANYCABLE_WEBSOCKET_URL!}?jid=${token}`, | ||
{ | ||
logLevel: "debug", | ||
}, | ||
); | ||
|
||
type CableContext = { | ||
cable: ReturnType<typeof getAuthenticatedCable>; | ||
}; | ||
|
||
const CableContext = createContext<CableContext | null>(null); | ||
|
||
export function useCable() { | ||
const context = useContext(CableContext); | ||
if (!context) { | ||
throw new Error("useCable must be used within a CableProvider"); | ||
} | ||
return context.cable; | ||
} | ||
|
||
export const CableProvider = ({ | ||
children, | ||
token, | ||
}: { children: React.ReactNode; token: string }) => { | ||
const cable = getAuthenticatedCable(token); | ||
|
||
return ( | ||
<CableContext.Provider value={{ cable }}>{children}</CableContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { broadcaster, identificator } from "@anycable/serverless-js"; | ||
import { signer } from "@anycable/serverless-js"; | ||
|
||
const secret = process.env.ANYCABLE_SECRET; | ||
const jwtTTL = "1h"; | ||
|
||
const broadcastURL = process.env.ANYCABLE_BROADCAST_URL!; | ||
const broadcastKey = process.env.ANYCABLE_BROADCAST_KEY!; | ||
|
||
export type Event = "notifications"; | ||
|
||
export async function getToken(userId: string) { | ||
if (!secret) { | ||
throw new Error("ANYCABLE_SECRET is not set"); | ||
} | ||
|
||
const identifier = identificator(secret, jwtTTL); | ||
const token = await identifier.generateToken({ userId }); | ||
return token; | ||
} | ||
|
||
export async function getStreamFor(room: Event, userId: string) { | ||
if (!secret) { | ||
throw new Error("ANYCABLE_STREAMS_SECRET is not set"); | ||
} | ||
|
||
const sign = signer(secret); | ||
const signedStreamName = sign(`${room}/${userId}`); | ||
|
||
return signedStreamName; | ||
} | ||
|
||
export async function broadcastEvent( | ||
room: Event, | ||
userId: string, | ||
message: Record<string, string | number>, | ||
) { | ||
const broadcastTo = broadcaster(broadcastURL, broadcastKey); | ||
await broadcastTo(`${room}/${userId}`, message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.