Skip to content

Instantly share code, notes, and snippets.

View thisisjofrank's full-sized avatar

Jo Franchetti thisisjofrank

View GitHub Profile
@thisisjofrank
thisisjofrank / Swarm5.ts
Created April 22, 2022 13:12
The onPresenceChanged callback will be called when a client joins or leaves the channel. Whenever this happens, we retrieve the latest full presence set from the channel instance (which is cached), trigger the election process, and finally call the onSwarmPresenceChanged callback.
private async onPresenceChanged() {
const members = await this.channel.presence.get();
this.ensureLeaderElected(members);
this.onSwarmPresenceChanged(members);
}
@thisisjofrank
thisisjofrank / Swarm4.ts
Created April 22, 2022 13:11
The connect method is going to subscribe to the presence event on the channel, passing the onSwarmPresenceChanged callback to be called when an event occurs. Once subscribed, we're going to enter presence, with a state of "connected", and call the onConnection callback:
public async connect() {
this.channel.presence.subscribe(this.onPresenceChanged.bind(this));
await this.channel.presence.enter('connected');
this.onConnection(this.channel);
}
@thisisjofrank
thisisjofrank / Swarm3.ts
Created April 22, 2022 13:10
he constructor takes a channelName to connect to, and generates an id for this client. In this implementation, we create a new Ably instance, and connect to the channelName which was given, passing a generated clientId.
constructor(channelName: string) {
this.id = Math.floor(Math.random() * 1000000) + "";
this.ably = new Ably.Realtime({ key: "api-key-here", clientId: this.id });
this.channel = this.ably.channels.get(channelName);
}
@thisisjofrank
thisisjofrank / Swarm2.ts
Created April 22, 2022 13:06
Next we define some properties to store callbacks. When connection and election events occur, we'll call the appropriate callback to allow the application to trigger an appropriate action:
public onElection: (channel: Types.RealtimeChannelPromise) => void = () => {};
public onConnection: (channel: Types.RealtimeChannelPromise) => void = () => {};
public onSwarmPresenceChanged: (members: Types.PresenceMessage[]) => void = () => {};
@thisisjofrank
thisisjofrank / Swarm1.ts
Created April 22, 2022 13:02
Let's start by defining a Swarm class in a file called Swarm.ts, and some member variables
import { Types } from 'ably';
import Ably from 'ably/promises';
export default class Swarm {
public readonly id: string;
private readonly ably: Ably.Realtime;
private readonly channel: Types.RealtimeChannelPromise;
@thisisjofrank
thisisjofrank / functionalcomponent.js
Created August 7, 2020 10:35
A functional component built with React that uses Ably
import React, { useState, useEffect } from 'react';
import Ably from "ably/promises";
const client = new Ably.Realtime("you-api-key-here");
const Component = (props) => {
const channel = client.channels.get('some-channel');
const [ messages, updateMessages ] = useState([]);
classCountComponent extends HTMLElement {
}
@thisisjofrank
thisisjofrank / QuestLogger.js
Last active April 21, 2021 16:10 — forked from Ugbot/QuestLogger.js
Logging from Ably into QuestDB
var net = require('net');
var path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3141;
const fetch = require("node-fetch");
@thisisjofrank
thisisjofrank / phases.csv
Last active September 1, 2020 10:31
Table of Depict-it game phases
Phase description phase name message 'kind'
Dealing and setup No messages
Collecting image input `drawing-request` instruction
Collecting image input response `drawing-response` drawing-response
Collecting caption input `caption-request` instruction
Collecting caption input response `caption-response` caption response
Collecting scores from players input `pick-one-request` instruction
Collecting scores from players response `pick-one-response` pick-one-response
`skip-scoring-forwards` skip-scoring-forwards
Displaying scores `show-scores` instruction
@thisisjofrank
thisisjofrank / component.js
Last active August 7, 2020 09:29
An example of a react component that uses Ably to publish to and subscribe to a channel
import React from 'react'
import Ably from "ably/promises";
const client = new Ably.Realtime("your-ably-api-key");
export default class AblyMessageComponent extends React.Component {
constructor() {
super();
this.state = { messages: [] };