-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
Conversation
e9d54d5
to
52e3244
Compare
52e3244
to
40895b7
Compare
for k, topic := range topics { | ||
connection := &subscription{ | ||
ID: "https://mercure.rocks/subscriptions/" + encodedTopics[k] + "/" + connectionID, | ||
Type: "https://mercure.rocks/Subscription", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]: {}}, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"})}) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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{})}) |
There was a problem hiding this comment.
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{})}) |
There was a problem hiding this comment.
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` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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/`) |
There was a problem hiding this comment.
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",`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
Co-Authored-By: Jacob Dreesen <jacob@hdreesen.de>
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.