Skip to content

Commit

Permalink
Update to Ably v2
Browse files Browse the repository at this point in the history
  • Loading branch information
hopperelec committed Apr 17, 2024
1 parent 293ceae commit 8bdf5c9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
24 changes: 15 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@prisma/client": "^5.11.0",
"ably": "^1.2.49",
"ably": "^2.0.2",
"google-auth-library": "^9.7.0",
"html-minifier-terser": "^7.2.0",
"panzoom": "^9.4.3",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/ably-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { browser } from "$app/environment";
export const ablyClientConnection =
browser && new ably.Realtime({ authUrl: "/ably-auth" });

const messages: { [key: string]: Readable<ably.Types.Message> } = {};
const messages: { [key: string]: Readable<ably.InboundMessage> } = {};

export function getChannel(name: string) {
if (!ablyClientConnection) return readable(undefined);
if (name in messages) return messages[name];
const ablyChannel = ablyClientConnection.channels.get(name);
const message = readable<ably.Types.Message>(undefined, (set) => {
ablyChannel.subscribe(set);
const message = readable<ably.InboundMessage>(undefined, (set) => {
ablyChannel.subscribe(set).then();
return () => {
ablyChannel.detach();
ablyChannel.detach().then();
ablyChannel.unsubscribe(set);
delete messages[name];
};
Expand Down
20 changes: 13 additions & 7 deletions src/lib/server/ably-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export function updateRealtimePoints(
) {
ablyServer.channels
.get("player:" + gameId + ":" + userId)
.publish("points", { points });
.publish("points", { points })
.then();
ablyServer.channels
.get("game:" + gameId + ":points")
.publish("points", { userId, points });
.publish("points", { userId, points })
.then();
}

export async function moveRoom(
Expand Down Expand Up @@ -60,7 +62,8 @@ export async function moveRoom(
.publish("move", {
userId: player.userId,
svgRef: room.svgRef,
});
})
.then();
}

export async function unclaimRooms(
Expand All @@ -73,8 +76,11 @@ export async function unclaimRooms(
roomId: { in: rooms.map((room) => Number(room.roomId)) },
},
});
ablyServer.channels.get("game:" + gameId + ":positions").publish(
"unclaim",
rooms.map((room) => Number(room.svgRef)),
);
ablyServer.channels
.get("game:" + gameId + ":positions")
.publish(
"unclaim",
rooms.map((room) => Number(room.svgRef)),
)
.then();
}
29 changes: 11 additions & 18 deletions src/routes/ably-auth/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ export const GET = async ({ locals }) => {
for (const player of userData.players) {
channels.push("game:" + player.gameId + ":*");
}
return new Promise((resolve, reject) => {
ablyServer.auth.createTokenRequest(
{
capability: channels.reduce(
(acc, channel) => {
acc[channel] = ["subscribe"];
return acc;
},
{} as { [key: string]: ["subscribe"] },
),
},
undefined,
(err, tokenRequest) => {
if (err) reject(err.message);
else resolve(json(tokenRequest));
},
);
});
return json(
await ablyServer.auth.createTokenRequest({
capability: channels.reduce(
(acc, channel) => {
acc[channel] = ["subscribe"];
return acc;
},
{} as { [key: string]: ["subscribe"] },
),
}),
);
};

0 comments on commit 8bdf5c9

Please sign in to comment.