We're using Pusher/channel to push a message from server to client. For instance:
- When a user type a new comment, the other user will receive this comment immediately.
- Or when a new user was added to a new project, he/she will be see it right after the manager update member list
To use it, in your backend code, import the pusherTrigger
and trigger an event as follows:
import { pusherTrigger } from '../../lib/pusher-server'
const eventName = `event-delete-task-comment`
pusherTrigger('team-collab', eventName, {
id,
triggerBy: updatedBy
})
Next, on the client side, create a new file in format as follow ui-app/app/_events/useEvent<event-name>.ts
// ui-app/app/_events/useEventDeleteComment.ts
import { usePusher } from './usePusher' // search usePusher in /ui-app
export const useEventDeleteComment = () => {
useEffect(() => {
const eventName = `event-delete-task-comment`
channelTeamCollab &&
channelTeamCollab.bind(eventName, (data) => {
console.log(data)
})
return () => {
channelTeamCollab && channelTeamCollab.unbind(eventName)
}
}, [channelTeamCollab])
}
To run a task in schedule, for example: run a task per 1h, run a task per Monday at 20h Do the following steps:
Open packages/be-gateway/src/events/index.ts
then create an event name and add a handler to it.
export const CHANNEL_DAY_STATS = 'stats:day-stats'
// We must subscribe channels first
redis.subscribe(CHANNEL_DAY_STATS)
// After that, we can listen messages from them
redis.on('message', async (channel: string, data: string) => {
if (channel === CHANNEL_DAY_STATS) {
const dayStats = new StatsByDayEvent()
dayStats.run()
}
})
Next, create the event handler at packages/be-gateway/src/events/
folder. Ex: stats.day.event.ts
export default class StatsByDayEvent {
constructor() {
}
async run() {
}
}
After registering event we need to publish message to trigger it. Open packages/be-scheduler/src/main.ts
and create a cronjob as follows
connectPubClient((err, redis) => {
if (err) return
// run every 20pm
const runAt20h = 'runAt20pm'
cronJob.create(runAt20h, '5 12,20 * * *', () => {
// Remember that, channel name must be same as Event name
const CHANNEL_DAY_STATS = 'stats:day-stats'
redis.publish(CHANNEL_DAY_STATS, 'heelo')
})
})
Name | Value | Desc | Required |
---|---|---|---|
NEXT_PUBLIC_FE_GATEWAY | http://localhost:4200/ | Frontend url | ✔️ |
NEXT_PUBLIC_BE_GATEWAY | http://localhost:3333/ | Backend api url | ✔️ |
NEXT_PUBLIC_APP_NAME | Namviek Dev | App name | ✔️ |
MONGODB_URL | mongodb+srv://:@/?retryWrites=true&w=majority | Database uri | ✔️ |
JWT_SECRET_KEY | 12GUY3N76U21d4IJ | Secret key | ✔️ |
JWT_REFRESH_KEY | 7us9s88o121ieeuo | Refresh key | ✔️ |
JWT_VERIFY_USER_LINK_TOKEN_EXPIRED | 1h | Expired time | ✔️ |
JWT_TOKEN_EXPIRED | 30m | Expired time | ✔️ |
JWT_REFRESH_EXPIRED | 4h | Expired time | ✔️ |
REDIS_HOST | Redis host | ✔️ |
Name | Value | Desc | Required |
---|---|---|---|
RESEND_TOKEN | Token for sending email. Visit: https://resend.com | ||
RESEND_EMAIL_DOMAIN | namviek.com | Email Domain | |
AWS_ACCESS_KEY | AWS s3 access key | ||
AWS_SECRET_ACCESS_KEY | AWS s3 secret key | ||
AWS_REGION | ap-southeast-1 | Aws region | |
AWS_S3_BUCKET | kampunistore | Aws bucket name |
Name | Value | Desc | Required |
---|---|---|---|
NEXT_PUBLIC_PUSHER_INSTANCE_ID | Pusher beam instance id | ||
PUSHER_INSTANCE_ID | Pusher beam instance id | ||
PUSHER_SECRET_KEY | Pusher beam secret key | ||
NEXT_PUBLIC_PUSHER_CHANNEL_APP_KEY | Push channel app key | ||
NEXT_PUBLIC_PUSHER_CHANNEL_APP_CLUSTER | ap1 | Pusher channel app cluster | |
PUSHER_CHANNEL_APP_ID | 1710577 | Pusher channel app id | |
PUSHER_CHANNEL_SECRET | Pusher channel secret |
Name | Value | Desc | Required |
---|---|---|---|
LIVEKIT_API_KEY | ANSWjslSNAwexMy | Livekit api key | |
LIVEKIT_API_SECRET | Livekit api secret | ||
NEXT_PUBLIC_LIVEKIT_URL | wss://namviek-hmunmehy.livekit.cloud | Livekit url |
Name | Value | Desc | Required |
---|---|---|---|
LOGTAIL_SOURCE_TOKEN | Logtail source token |
Name | Value | Desc | Required |
---|---|---|---|
FIREBASE_PROJECT_ID | namviek-4234 | Project id | |
FIREBASE_CLIENT_EMAIL | [email protected] | Client email generated by Firebase | |
FIREBASE_PRIVATE_KEY | Firebase private key |