1.) Is it possible to keep/cache some simple data within the scope of
a subscribe/unsubscribe session?
2.) Does the API support a query asking whether at least one client
has subscribed to channel xyz?
Thanks!
I appreciate the excellent work!
1.) Is it possible to keep/cache some simple data within the scope of
a subscribe/unsubscribe session?
2.) Does the API support a query asking whether at least one client
has subscribed to channel xyz?
On Dec 20, 12:15 am, James Coglan <jcog...@gmail.com> wrote:
> On 19 December 2011 23:10, mstu...@googlemail.com <mstu...@googlemail.com>wrote:
>
> > 1.) Is it possible to keep/cache some simple data within the scope of
> > a subscribe/unsubscribe session?
>
> I'm not sure what you mean by 'cache' here -- could you give more details
> on what your application needs to do?
Socket.IO (http://socket.io/#how-to-use -> "Storing data associated to
a client" (somwhere in the middle)) allows to store data associated
with a client in the session. Of course, storing/cleaning the data
could by managed on the application layer, but application logic could
be saved if Faye manages the data lifecycle, because it knows best
when a session starts and ends. Such a feature would make sense for
storing simple data..
> > 2.) Does the API support a query asking whether at least one client
> > has subscribed to channel xyz?
>
> No, but you can easily track this yourself by listening to
> :subscribe/:unsubscribe events:
>
> faye.bind :subscribe do |client_id, channel|
> # increment count for channel
> end
>
> faye.bind :unsubscribe do |client_id, channel|
> # decrement count for channel
> end
Sure, but it requires me keeping tracking of this data. Faye has to
keep track of this data anyway, so reusing it by using some query
interface instead of duplicating it at the application layer doesn't
feel DRY.
Socket.IO (http://socket.io/#how-to-use -> "Storing data associated to
a client" (somwhere in the middle)) allows to store data associated
with a client in the session. Of course, storing/cleaning the data
could by managed on the application layer, but application logic could
be saved if Faye manages the data lifecycle, because it knows best
when a session starts and ends. Such a feature would make sense for
storing simple data..
> > 2.) Does the API support a query asking whether at least one client
> > has subscribed to channel xyz?
Sure, but it requires me keeping tracking of this data. Faye has to
keep track of this data anyway, so reusing it by using some query
interface instead of duplicating it at the application layer doesn't
feel DRY.
server.bind :handshake do |client_id|# client ID was just createdendserver.bind :disconnect do |client_id|# client ID session endedend
when is the disconnect event fired? Just after page close, or does it have a built in time-out feature?
I just started with pub/sub and thus, don't have any experience yet,
but what you are saying sounds all reasonable to me. So, I take that
as an advice.
I don't want to go in too much detail what I am working on, but I can
give an example which is very close from the "problem I am trying to
solve" point of view.
So, assuming we have a group calendar and 10 users have access to it
and view it at the same time. Now, user one is creating a new event
and I want to broadcast this event to the other users,
So, when a user views the calendar, it subscribes to the relevant
channel. I was thinking about using a resource-like style for channel
naming, for example, the channel for viewing the calendar could be
called "calendars/#calendar_id/show". Each user having access to
calendar with id #calendar_id subscribes to channel "calendars/
#calendar_id". If one user adds a new event, it is published to a
different channel, called "event/#event_id/create", with the
javascript to add the event to the DOM as payload. There is no client
who has subscribed to this channel, but I thought it's cool to listen
to this event on the server side, identifying it by using some pattern
matching and then publish the payload to "calendars/#calendar_id/
show". One might say why not publishing to channel "calendars/
#calendar_id/show" directly. To me it seems important to decouple the
creation of the calendar event from the rest, so I can later publish
to multiple channels, e.g. besides publishing to "calendars/
#calendar_id/show" also publishing to "users/#user_id/activity_stream/
show" and so on.
The reason why I asked (question 1) whether I can store data in the
session managed by Faye was to further improve the implementation by
only publishing data to clients who are actually affected by the
calendar change (the new event). For instance, if a user is viewing
week 35 and the new calendar event is week 36, it doesn't make sense
to send any data related to the new event to the user, because it
won't be displayed anyway. So, if any client would send some meta data
refering to its current view (simply start date and end date) and I
could store this data in the session, then I could filter new events
and only forward them if it makes sense. But yes, this would require
each client to subscribe to a different channel, so one could call
them "user_id:calendars/#calendar_id/show", basically namespacing them
using #user_id. One the server side, I would then have to publish to
each channel, by iterating over all users associated with a specific
calendar.
The reason why I asked whether I can query a certain channel (question
2) was related to the fact that I don't have to have to publish to a
certain channel / user namespaced channel, e.g. "1234:calendars/
#calendar_id/show", if user with id 1234 hasn't subscribed to this
channel...
So, what do you think? Am I overcomplicating things here?
Feedback is very much appreciated...
So, assuming we have a group calendar and 10 users have access to it
and view it at the same time. Now, user one is creating a new event
and I want to broadcast this event to the other users,
So, when a user views the calendar, it subscribes to the relevant
channel. I was thinking about using a resource-like style for channel
naming, for example, the channel for viewing the calendar could be
called "calendars/#calendar_id/show". Each user having access to
calendar with id #calendar_id subscribes to channel "calendars/
#calendar_id". If one user adds a new event, it is published to a
different channel, called "event/#event_id/create", with the
javascript to add the event to the DOM as payload. There is no client
who has subscribed to this channel, but I thought it's cool to listen
to this event on the server side, identifying it by using some pattern
matching and then publish the payload to "calendars/#calendar_id/
show". One might say why not publishing to channel "calendars/
#calendar_id/show" directly. To me it seems important to decouple the
creation of the calendar event from the rest, so I can later publish
to multiple channels, e.g. besides publishing to "calendars/
#calendar_id/show" also publishing to "users/#user_id/activity_stream/
show" and so on.
The reason why I asked (question 1) whether I can store data in the
session managed by Faye was to further improve the implementation by
only publishing data to clients who are actually affected by the
calendar change (the new event). For instance, if a user is viewing
week 35 and the new calendar event is week 36, it doesn't make sense
to send any data related to the new event to the user, because it
won't be displayed anyway. So, if any client would send some meta data
refering to its current view (simply start date and end date) and I
could store this data in the session, then I could filter new events
and only forward them if it makes sense.
The reason why I asked whether I can query a certain channel (question
2) was related to the fact that I don't have to have to publish to a
certain channel / user namespaced channel, e.g. "1234:calendars/
#calendar_id/show", if user with id 1234 hasn't subscribed to this
channel...
So, what do you think? Am I overcomplicating things here?
> A better approach is to just treat channels as topics that related toOk, but this would require much more logic on the client side. Data
> changes in your data model. So, /calendars/:id is a good one -- any part of
> the UI can listen to this to find out when the calendar changes, and do its
> bit of work to update the UI.
model changes would have to published as raw data, e.g. JSON, send to
the client and it would have to decide whether and how to process it.
I actually think this isn't really Rails like, e.g. take a simple
Model update send asynchronuously to the server. In the response,
wouldn't Rails just return JS responsible for updating particular
parts of the UI rather than sending just raw data and let the UI do
the rest?
> The parts of code you currently have thatYes, but I don't see a way to avoid event dispatching on the server
> listen to /calendars/:id/show, /users/:id/activity_stream/show should all
> just be listening to data changes that affect them, not receiving pieces of
> JavaScript to execute.
side. Let's take the example of some users viewing the calendar (1)
and some other users viewing their personal profile showing an
activity stream (2).
It makes sense to me that (1) subscribe to channel "calendar/:id" but
what about (2)? Users can have zero or more calendars, so when they
view their profile and thus activity stream, they would have to
subscribe to many channels, in this scenario, one per calendar...
In the other approach, they would just have to subscribe to one
channel and what's going to be published on that channel can be nicely
configured in a central place on the server side.
Based on your experience, is event dispatching on the server side a no
go?