Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support presence message extras #1418

Merged
merged 2 commits into from
Aug 8, 2023
Merged

Support presence message extras #1418

merged 2 commits into from
Aug 8, 2023

Conversation

lmars
Copy link
Member

@lmars lmars commented Aug 3, 2023

As per RTP8a and TP3i, presence messages support the extras field, but there is currently no way to set presence extras in the API (even though the IDL indicates the enter function signature as enter(Data?, extras?: JsonObject) => io).

Rather than making a breaking change to the enter function, this PR introduces an enterMessage function which supports passing a PresenceMessage, similar to how publish supports passing a Message, for example:

channel.presence.enterMessage(PresenceMessage.fromValues({
  extras: { headers: { key: 'value' } },
}));

This functionality is required for an upcoming release of the Spaces SDK.

MMB-212

@lmars lmars requested review from dpiatek and owenpearson August 3, 2023 00:00
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 3, 2023 00:00 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 3, 2023 00:02 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 3, 2023 00:02 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 3, 2023 00:22 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 3, 2023 00:23 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 3, 2023 00:23 Inactive
@lmars lmars force-pushed the presence-extras branch from 0075a6c to eac58f4 Compare August 3, 2023 00:30
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 3, 2023 00:30 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 3, 2023 00:31 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 3, 2023 00:31 Inactive
@lmars lmars force-pushed the presence-extras branch from eac58f4 to 2956855 Compare August 3, 2023 00:39
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 3, 2023 00:39 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 3, 2023 00:41 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 3, 2023 00:41 Inactive
@paddybyers
Copy link
Member

It's unfortunate to have the asymmetry of having publish(Message) but no enter(PresenceMessage), but I don't see an easy way around that whilst remaining compatible.

Copy link
Member

@owenpearson owenpearson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the features spec, extras should be the second argument to enter, leave, update, similarly it's supposed to be the final argument to enterClient et al, is there a reason for breaking from the spec here?

@lmars
Copy link
Member Author

lmars commented Aug 3, 2023

extras should be the second argument to enter, leave, update, similarly it's supposed to be the final argument to enterClient et al, is there a reason for breaking from the spec here?

A few reasons:

  1. I don't think the spec should be so prescriptive about function signatures, I think it's enough to say "there should be a function which supports entering a channel with data and/or extras" and leave it up to each SDK to implement that in whatever way seems reasonable (this was discussed internally in ADR57), so coming from working on ably-go and ably-rust, I personally ignore the IDL in the spec
  2. I think the API should be closer to publish which either takes name+data or a Message, and I also don't like the function overloading pattern in general
  3. I wanted to avoid a breaking change, unless you think this can be supported in a non-breaking way?

@owenpearson
Copy link
Member

owenpearson commented Aug 3, 2023

@lmars fair enough, i don't disagree with (1) and (2). As for supporting this without a breaking change we could support both signatures for enter using instanceof?

enter(data: any): Promise<void>;
enter(presenceMessage: PresenceMessage): Promise<void>;

enter(dataOrPresenceMessage: any): Promise<void> {
  if (dataOrPresenceMessage instanceof PresenceMessage) {
    // normalise arguments
  }
  // ...
}

i guess the only way this would be breaking would be if someone is already using a PresenceMessage instance as presence data but i think we can safely assume no one would actually do that.

Copy link
Member

@owenpearson owenpearson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also - any changes/additions to the public api need to be added to ably.d.ts, so with the changes in their current state we would need to add the enterMessage signature.

@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 4, 2023 14:24 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 4, 2023 14:26 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 4, 2023 14:26 Inactive
@lmars
Copy link
Member Author

lmars commented Aug 4, 2023

@owenpearson ah thanks, I wasn't aware you could do x instanceof y, updated to use that approach in 76624d4, meaning this now supports:

presence.enter(PresenceMessage.fromValues({
  data,
  extras: { headers: { key: 'value' } },
}));

Given that data has type any in ably.d.ts, I assume nothing needs to change there?

@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 6, 2023 10:15 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 6, 2023 10:16 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 6, 2023 10:17 Inactive
@lmars lmars force-pushed the presence-extras branch from 2a6ad90 to 7d73c50 Compare August 6, 2023 11:47
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 6, 2023 11:48 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 6, 2023 11:49 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 6, 2023 11:49 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 6, 2023 12:31 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 6, 2023 12:33 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 6, 2023 12:33 Inactive
@lmars lmars force-pushed the presence-extras branch from e6aad42 to 7d73c50 Compare August 6, 2023 13:09
@github-actions github-actions bot temporarily deployed to staging/pull/1418/features August 6, 2023 13:10 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/bundle-report August 6, 2023 13:11 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1418/typedoc August 6, 2023 13:11 Inactive
@owenpearson
Copy link
Member

Given that data has type any in ably.d.ts, I assume nothing needs to change there?

Yeah I guess this means we can't add a new signature, would be good to update the docstrings to mention this though

@lmars
Copy link
Member Author

lmars commented Aug 8, 2023

@owenpearson

would be good to update the docstrings to mention this though

Done in 5baf94d.

Comments addressed, PTAL.

Copy link
Member

@owenpearson owenpearson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@lmars lmars merged commit 40f890b into main Aug 8, 2023
@lmars lmars deleted the presence-extras branch August 8, 2023 17:53
* Initialises a `PresenceMessage` from a `PresenceMessage`-like object.
*
* @param values - The values to intialise the `PresenceMessage` from.
* @param stringifyAction - Whether to convert the `action` field from a number to a string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be exposing things like a stringifyAction param in the public api. There is no reason a user of the lib should have to know or care that we use numerical encoding in our internal wire protocol, and this doc string is meaningless to such a user.

I'm not a fan of having the one method to do conceptually quite different actions controlled by that param anyway, so in #1923 I've split it into two different methods, fromValues(values: Properties<PresenceMessage>): Message suitable for public use, and fromWireProtocol(values: WireProtocolPresenceMessage): PresenceMessage as an internal method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I didn't introduce the argument, I just needed the function to be included here to be able to call it from Spaces, I paid very little attention to whether or not the argument is suitable, what you suggest sounds good to me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants