microsub icon indicating copy to clipboard operation
microsub copied to clipboard

Streaming API

Open aaronpk opened this issue 7 years ago • 8 comments

Clients should have a way to tap in to some sort of streaming API to get updates from the server in realtime.

I've had good luck with the EventSource API on both the client and server side of it. We should define a list of event types that map to the different Microsub actions, so that there's a way for the server to push new events to the client. Things like:

  • channel name and unread count when a post is added to the channel
  • new entries added to a channel (maybe only when a channel is viewed? should all posts be pushed to the client? should the client request entries per channel separately?)
  • what else?

aaronpk avatar Feb 18 '18 01:02 aaronpk

Regarding new entries, it should be only for the ones that a client chooses to subscribe to (this way, you can subscribe to notifications, a “catch-all” timeline and maybe one crafted particularity for an event).

(Originally published at: https://v2.jacky.wtf/post/6fd5c090-e0be-4030-9267-faf5517b6fcd)

jalcine avatar Apr 14 '19 21:04 jalcine

@aaronpk what about WebSockets?

I'm thinking along the lines of this:

  • add an api call like 'socket' which returns wss:// url, that would accept the same rpc
  • add api call 'authorize(token)' that allows user to authorize web socket connection
  • add api call 'stream(channel)' that starts streaming events that could be jf2 objects.

Another case to be handled somehow is updating the posts that client already has (edits, webmentions count, etc.)

I'm going to experiment with this in a few weeks.

timmarinin avatar Aug 12 '19 14:08 timmarinin

I have tried building the server and client part for EventSource in Ekster, but found that it was quite difficult to get this working with authentication because it was hard to add Authorization headers.

pstuifzand avatar Aug 12 '19 18:08 pstuifzand

The thing I like about EventSource is it's still HTTP, and doesn't require adding another client library into the project (like Websockets). Websockets is also technically overkill since we don't need bi-directional communication for this, just one way.

aaronpk avatar Aug 12 '19 18:08 aaronpk

@aaronpk: with bi-directional you can send commands for pausing the stream, e.g., when switching to another tab.

timmarinin avatar Aug 12 '19 18:08 timmarinin

I tried to implement something with EventSource. EventSource itself works great, but I'm not sure how to authenticate it.

This video shows how I can update the unread count of the Home channel. https://www.youtube.com/watch?v=U6gJS9nS220

pstuifzand avatar Aug 17 '19 14:08 pstuifzand

Here is another video where the items are added automatically to the current timeline. The post is added by curl using Micropub. https://www.youtube.com/watch?v=tKhBKAqqBBs

pstuifzand avatar Aug 17 '19 14:08 pstuifzand

I've been experimenting with the idea of real-time updates in Lwa. It's still rough around the edges but I'm planning to implement a bit of what I mentioned on my site, namely:

{
   "name": "Notifications",
   "uid": "notifications",
   "subscriptions": [
     {"type": "websocket", url: "wss://lwa.black.af/endpoints/microsub/ws?action=timeline&channel=notifications"},
     {"type": "eventsource", url: "https://lwa.black.af/endpoints/eventsource/channels/notifications"}
   ]
}

That route could be authenticated the same way as the Microsub endpoint. I made a change to the sample in my original post so that the 'plumbing' needed to connect and subscribe was explicit - less guesswork for clients.

jalcine avatar Sep 20 '19 20:09 jalcine