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

Dispatch connection and disconnection events #238

Merged
merged 2 commits into from
Feb 24, 2020
Merged

Conversation

dunglas
Copy link
Owner

@dunglas dunglas commented Feb 18, 2020

This PR adds a section in the spec and a new option to dispatch updates every time a subscription is created or terminated. It is useful when subscribers have to to be aware of the other currently connected subscribers.

When #100 will be implemented, this feature will also allow servers to be notified when a user is online or offline, which is a frequent enhancement request.

Closes #28, #117.

for k, topic := range topics {
connection := &subscription{
ID: "https://mercure.rocks/subscriptions/" + encodedTopics[k] + "/" + connectionID,
Type: "https://mercure.rocks/Subscription",
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think that the hostname could be made configurable in the future?

Copy link
Owner Author

@dunglas dunglas Feb 18, 2020

Choose a reason for hiding this comment

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

It's not a host name, it's a RDF type. It's similar to Schema.org's types: their IDs never change. TL;DR: it's fixed on purpose.

Copy link
Contributor

Choose a reason for hiding this comment

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

okay then.


for k, topic := range topics {
connection := &subscription{
ID: "https://mercure.rocks/subscriptions/" + encodedTopics[k] + "/" + connectionID,

Choose a reason for hiding this comment

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

Put https://mercure.rocks/subscriptions or at least https://mercure.rocks in constant or in env var setup

Copy link
Owner Author

Choose a reason for hiding this comment

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

#238 (comment)

It's a fixed value, on purpose so an env var isn't applicable. IMHO using a constant isn't worth it because every value is used only 1 time.


u := &Update{
Topics: []string{connection.ID},
Targets: map[string]struct{}{"https://mercure.rocks/targets/subscriptions": {}, "https://mercure.rocks/targets/subscriptions/" + encodedTopics[k]: {}},

Choose a reason for hiding this comment

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

Same here

go func() {
// Authorized to receive connection events
defer wg.Done()
req := httptest.NewRequest("GET", defaultHubURL+"?topic=https://mercure.rocks/subscriptions/{topic}/{connectionID}", nil).WithContext(ctx1)

Choose a reason for hiding this comment

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

Use domain or base url constant here

// Authorized to receive connection events
defer wg.Done()
req := httptest.NewRequest("GET", defaultHubURL+"?topic=https://mercure.rocks/subscriptions/{topic}/{connectionID}", nil).WithContext(ctx1)
req.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyAuthorizedJWT(hub, subscriberRole, []string{"https://mercure.rocks/targets/subscriptions"})})

Choose a reason for hiding this comment

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

Same here

go func() {
// Not authorized to receive connection events
defer wg.Done()
req := httptest.NewRequest("GET", defaultHubURL+"?topic=https://mercure.rocks/subscriptions/{topic}/{connectionID}", nil).WithContext(ctx2)

Choose a reason for hiding this comment

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

Same here

// Not authorized to receive connection events
defer wg.Done()
req := httptest.NewRequest("GET", defaultHubURL+"?topic=https://mercure.rocks/subscriptions/{topic}/{connectionID}", nil).WithContext(ctx2)
req.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyAuthorizedJWT(hub, subscriberRole, []string{})})

Choose a reason for hiding this comment

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

Setup constant cookie name with value mercureAuthorization


ctx, cancelRequest2 := context.WithCancel(context.Background())
req := httptest.NewRequest("GET", defaultHubURL+"?topic=https://example.com", nil).WithContext(ctx)
req.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyAuthorizedJWT(hub, subscriberRole, []string{})})

Choose a reason for hiding this comment

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

Same here

* `@id`: the identifier of this update, it **MUST** be the same value as the subscription update's
topic

* `@type`: the fixed value `https://mercure.rocks/Subscription`
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I agree that this should be a fixed value, it should at least be configurable. I mean what happens if you want to change the name, or you get hit by a bus and we can't get the domain?

Copy link
Owner Author

Choose a reason for hiding this comment

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

See my previous comment. It's not related to hostnames, domains and the like. It's a RDF type. It's similar to a XML namespace. The value never changes, and doesn't have to be dereferencable (the URL may not exist).


assert.Equal(t, http.StatusOK, resp.StatusCode)
bodyContent := string(body)
assert.Contains(t, bodyContent, `data: "@id": "https://mercure.rocks/subscriptions/https%3A%2F%2Fexample.com/`)

Choose a reason for hiding this comment

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

Same here

assert.Equal(t, http.StatusOK, resp.StatusCode)
bodyContent := string(body)
assert.Contains(t, bodyContent, `data: "@id": "https://mercure.rocks/subscriptions/https%3A%2F%2Fexample.com/`)
assert.Contains(t, bodyContent, `data: "@type": "https://mercure.rocks/Subscription",`)

Choose a reason for hiding this comment

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

Same here

docs/hub/config.md Outdated Show resolved Hide resolved
Co-Authored-By: Jacob Dreesen <jacob@hdreesen.de>
@dunglas dunglas merged commit 94f8813 into master Feb 24, 2020
@dunglas dunglas deleted the connection-events branch February 24, 2020 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connection events
4 participants