bytesandbutter / pg-crud

a postgresql crud layer
MIT License
1 stars 0 forks source link

notifications api #8

Open qwesda opened 9 years ago

qwesda commented 9 years ago

the clients should be able to register themselves for notifications

the workflow might be as follows:

var genresFeed  = EventSource("_notifications/genres");
var formatsFeed = EventSource("_notifications/formats");
var labelsFeed  = EventSource("_notifications/labels");
var watchedArtistsFeed  = EventSource("_notifications/artists/234,435643,4534");
var watchedReleasesFeed = EventSource("_notifications/release/435,4325,6547567,232,432,4235");

a change notification should be the following tuples:

ibotty commented 9 years ago

let's evaluate server sent events. they might be what we really want. and might be faster as well.

https://developer.mozilla.org/en-US/docs/Server-sent_events/EventSource

(that was the tech i had in mind the day before yesterday but could not remember.)

ibotty commented 9 years ago

ah. there is a polyfill for internet explorer: https://github.com/remy/polyfills/blob/master/EventSource.js

qwesda commented 9 years ago

if I understand correctly this will only handle messages from the server to the client. how should the client subscribe to notifications?

qwesda commented 9 years ago

the only possibility i see with SSE would be for _register_fortypes new EventSource("_notifications/<resource_type>"); – closing the connection would be unsubscribing.

But how should the client subscribe ti individual ids? new EventSource("_notifications/<resource_type>/234,456,767,8678,769,43,54,534,645788,678"); or the same thing with ?ids= both are a tad ugly ... but ok with me

ibotty commented 9 years ago

this is a limitation indeed. in the end it's another ad-hoc registration. can we live with it or do we need the extra flexibility?

sse might be a little more expensive (more open connections) when listening to many different notifications.

with an ie polyfill browser support is identical (or in favor of sse, i did dont bother checking older ie) comparing to websockets.

sse are a little easier to implement server-side. client-side at least reconnection logic is handled by sse and not by websockets (of course that is not that big a deal). dispatching the different kind of notifications will be left to the client js, but that shouldn't be a big deal.

broken proxies between the server and the client might not support websockets but sse's. i don't consider that very relevant though.

(coloring the bike shed purple i favor "releases/id1,id2,id3" as channel name)

ibotty commented 9 years ago

in all: i guess i'll leave it for you to decide.

qwesda commented 9 years ago

EventSource("_notifications/<resource_type>/id1,id2,id3"); is fine by me. The server should then pass the messages like this:

event: relationCreated
data: ["releases", 24, "releses_artists", {"artist_id": 34, "release_id": 24, "role": "main artist"}]
qwesda commented 9 years ago

I wouldn't bother with the polyfill right now since no one has an IE to test it anyway

ibotty commented 9 years ago

so it is server sent event then?

i'd say for all resources of type res it should simply be

  _notifications/res

(with or without trailing slash.)

k?

ibotty commented 9 years ago

let's just disallow text primary keys with ,. they won't work with that protocol.

qwesda commented 9 years ago

primary keys should only be of type int or uuid - no text primary keys allowed

ibotty commented 9 years ago

disregarding cartesian product of primary keys which is very useful, i agree for database layout in general. but pg-crud should support strange primary keys as well. enums and even text might make sense for some applications as well.

that's a little besides the point though.

qwesda commented 9 years ago

updated spec

ibotty commented 9 years ago

we might think about how to handle event ids. the following scheme might work for connections losses as well as suspended (i.e. inactive) tabs.

that way, the client does not need to re-get all resources of interest when nothing changed after resuming.

i suppose there is a way to specify last event id from the browser apis when opening a eventsource connection. i have not checked that. edit: but there is not.

qwesda commented 9 years ago

I don't think this will be necessary. If a tab gets deactivated the client will close the SSE-connections. If a tab gets reactivated i will request the visible data again from the server and reestablish the SSE-connection.

ibotty commented 9 years ago

i wanted to avoid the need to retrieve the information for every tab switching.

ibotty commented 9 years ago

a resume of our phone call is, that

there is still the question on how that much policy will work generically (i.e without hard-wiring up the notify channel name).