-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🚀Realtime presence
- Loading branch information
Showing
11 changed files
with
193 additions
and
205 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,89 @@ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
import Pusher from "pusher-js"; | ||
import { useEffect } from "react"; | ||
import type { z } from "zod"; | ||
import { useEffect, useState } from "react"; | ||
import { z } from "zod"; | ||
|
||
import { env } from "../env/client.mjs"; | ||
|
||
export default function useSocket<T extends z.Schema>( | ||
const PresenceInfoSchema = z.object({ | ||
name: z.string().nullish(), | ||
email: z.string().nullish(), | ||
image: z.string().nullish(), | ||
}); | ||
|
||
const PresenceSubscriptionSucceededSchema = z.object({ | ||
count: z.number(), | ||
me: z.object({ | ||
id: z.string(), | ||
info: PresenceInfoSchema, | ||
}), | ||
members: z.record(PresenceInfoSchema), | ||
}); | ||
|
||
const PresenceMemberEventSchema = z.object({ | ||
id: z.string(), | ||
info: PresenceInfoSchema, | ||
}); | ||
|
||
type PresenceInfo = z.infer<typeof PresenceInfoSchema>; | ||
|
||
export default function useSocket<T extends z.Schema, R extends z.Schema>( | ||
channelName: string, | ||
eventSchema: T, | ||
callback: (data: z.infer<T>) => void | ||
accessToken: string | undefined, | ||
callbacks: { | ||
event: string; | ||
callback: (data: unknown) => Promise<void> | void; | ||
}[] | ||
) { | ||
const [members, setMembers] = useState<Record<string, PresenceInfo>>({}); | ||
|
||
useEffect(() => { | ||
const app_key = env.NEXT_PUBLIC_PUSHER_APP_KEY; | ||
if (!app_key) return () => void 0; | ||
if (!app_key || !accessToken) return () => void 0; | ||
|
||
const pusher = new Pusher(app_key, { cluster: "mt1" }); | ||
const channel = pusher.subscribe(channelName); | ||
const pusher = new Pusher(app_key, { | ||
cluster: "mt1", | ||
channelAuthorization: { | ||
transport: "ajax", | ||
endpoint: `${env.NEXT_PUBLIC_BACKEND_URL}/api/auth/pusher`, | ||
headers: { | ||
Authorization: `Bearer ${accessToken || ""}`, | ||
}, | ||
}, | ||
}); | ||
|
||
console.log("subscribed to channel", channelName); | ||
const channel = pusher.subscribe("presence-" + channelName); | ||
callbacks.map(({ event, callback }) => { | ||
channel.bind(event, async (data) => { | ||
await callback(data); | ||
}); | ||
}); | ||
|
||
channel.bind("my-event", async (data) => { | ||
const obj = (await eventSchema.parse(data)) as z.infer<T>; | ||
callback(obj); | ||
channel.bind("pusher:subscription_succeeded", async (data) => { | ||
const event = await PresenceSubscriptionSucceededSchema.parseAsync(data); | ||
setMembers(event.members); | ||
}); | ||
|
||
channel.bind("pusher:member_added", async (data) => { | ||
const event = await PresenceMemberEventSchema.parseAsync(data); | ||
|
||
setMembers((prev) => ({ | ||
...prev, | ||
[event.id]: event.info, | ||
})); | ||
}); | ||
|
||
channel.bind("pusher:member_removed", async (data) => { | ||
const event = await PresenceMemberEventSchema.parseAsync(data); | ||
setMembers(({ [event.id]: _, ...rest }) => rest); | ||
}); | ||
|
||
return () => { | ||
pusher.unsubscribe(channelName); | ||
pusher.unsubscribe(channel.name); | ||
pusher.disconnect(); | ||
console.warn("unsubscribed from channel", channelName); | ||
setMembers({}); | ||
}; | ||
}, []); | ||
}, [accessToken]); | ||
|
||
return members; | ||
} |
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
Oops, something went wrong.