Oouhammmourachid /
Public
ouhammmourachid / realtime-chat
PublicPublic and private chat rooms with realtime messages, attachments and edit tracking. Subscribe with the PocketBase SDK and messages arrive live.
- Realtime
- 0 deploys
- 0 forks
Realtime Chat: rooms, members and messages
Public and private chat rooms with realtime messages, attachments and edit tracking. Subscribe with the PocketBase SDK and messages arrive live.
What you get
- Public rooms anyone signed in can join; private rooms for members only
- Realtime:
pb.collection('messages').subscribe('*')respects the same access rules - Messages always sent as the signed-in user; only members can post in private rooms
- Edited messages are flagged automatically
- Up to 5 attachments per message
POST /api/rooms/{id}/joinand/leaveendpoints
Collections
rooms
| Field | Type | Notes |
|---|---|---|
name (required) | text | |
description | text | |
is_private | bool | Members only |
owner (required) | relation → users | Set by a hook to the signed-in user |
members | relation → users (many) | The owner is always a member |
| Action | Who can do it |
|---|---|
| List / view | @request.auth.id != '' && (is_private = false || members ?= @request.auth.id) |
| Create | signed-in users |
| Update | owner = @request.auth.id |
| Delete | owner = @request.auth.id |
messages
| Field | Type | Notes |
|---|---|---|
room (required) | relation → rooms | |
author (required) | relation → users | Set by a hook to the signed-in user |
text | text | |
attachments | file | Up to 5 files, 10 MB each |
edited | bool | Set by a hook |
| Action | Who can do it |
|---|---|
| List / view | @request.auth.id != '' && (room.is_private = false || room.members ?= @request.auth.id) |
| Create | signed-in users |
| Update | author = @request.auth.id |
| Delete | author = @request.auth.id |
Hooks
All hooks are in pb_hooks/main.pb.js and commented.
Quick start
import PocketBase from "pocketbase"
const pb = new PocketBase("https://YOUR-INSTANCE.pocketshub.com")
const room = await pb.collection("rooms").create({ name: "general" })
// Live messages for this room:
pb.collection("messages").subscribe("*", (e) => {
if (e.record.room === room.id) console.log(e.action, e.record.text)
}, { expand: "author" })
await pb.collection("messages").create({ room: room.id, text: "Hello!" })
// History, newest first:
const history = await pb.collection("messages").getList(1, 50, {
filter: `room = "${room.id}"`, sort: "-created", expand: "author",
})
Notes
- Private rooms: the owner adds people by updating
members(e.g.{ "members+": userId }).
License
MIT
2 collections, 1 relation. Hover a collection to trace its relations