percolatestudio / publish-counts

Meteor package to help you publish the count of a cursor, in real time
https://atmospherejs.com/tmeasday/publish-counts
MIT License
200 stars 46 forks source link

Server Support #80

Open SachaG opened 8 years ago

SachaG commented 8 years ago

Since Counts is only defined on the client, the package doesn't work with server-side rendering. Maybe I could submit a PR for that?

tmeasday commented 8 years ago

I'm not quite sure it makes sense, because the name -> count mapping comes from the publication body.

SachaG commented 8 years ago

Hmm that's true… I guess I'll do this for now then:

    const totalCount = Meteor.isClient ? Counts.get(this.props.publication) : Posts.find().count();
tmeasday commented 8 years ago

Yup!

On Fri, 19 Feb 2016 at 14:06 Sacha Greif notifications@github.com wrote:

Hmm that's true… I guess I'll do this for now then:

const totalCount = Meteor.isClient ? Counts.get(this.props.publication) : Posts.find().count();

— Reply to this email directly or view it on GitHub https://github.com/percolatestudio/publish-counts/issues/80#issuecomment-186026474 .

SachaG commented 8 years ago

I guess if we wanted to solve this in a cleaner way, we'd have to register the count outside of the publication, and then only reference it inside the publication? Or maybe not have it in the publication at all?

tmeasday commented 8 years ago

Looks like Arunodas thing works different to how I thiught On Fri, 19 Feb 2016 at 4:52 PM, Sacha Greif notifications@github.com wrote:

I guess if we wanted to solve this in a cleaner way, we'd have to register the count outside of the publication, and then only reference it inside the publication? Or maybe not have it in the publication at all?

— Reply to this email directly or view it on GitHub https://github.com/percolatestudio/publish-counts/issues/80#issuecomment-186069259 .

tmeasday commented 8 years ago

You should definitely give it a try

SachaG commented 8 years ago

I don't know if this is related to the latest FlowRouterSSR or if this has been happening all along and I just now noticed it, but calling Counts.publish() inside a publication seems to be preventing FlowRouter from subscribing to that publication when performing SSR.

@arunoda any thoughts about this?

boxofrox commented 8 years ago

@SachaG, could you resubmit the FlowRouterSSR problem as a separate issue? It'll certainly help users with the same problem that may not realize this discussion is buried under a closed topic.

sungwoncho commented 8 years ago

I am having the same issue as @SachaG. Basically, using FlowRouter SSR, on the server side, I get:

Error when doing SSR. path:/: Object [object Object] has no method 'get'

I thought about moving this file to server, but Counts is already defined in the server. I'd like to help resolve this. How can I proceed?

boxofrox commented 8 years ago

@sungwoncho sorry for the late response, I'm currently out of town. Unfortunately I'm not familiar with FlowRouter SSR, so I'm restricted to one technique for troubleshooting at this time. I need a code example using publish-counts and flowrouter-ssr that reproduces this issue, with instructions explaining how to use the app to produce the error and what the expected result is. A github repo of a meteor app is most preferred as the example, though it need not be the project you're working on. A new meteor app project that only produces the error is sufficient.

If you can provide that then I can dig further.

Otherwise, your best bet is likely to code isServer/Client checks that swap out publish-counts functions for a server-only variation.

SachaG commented 8 years ago

I was actually thinking about this again yesterday and I think creating a server-only Counts.get() function would work.

The main issue is how to store the counts. We could put them in an object inside Counts on the server, but they need to be identified with a unique session-specific variable. And we probably also need a way to clean them up when the session ends to avoid having that object become huge?

SachaG commented 8 years ago

Also just to clarify, there's two separate issues here (my bad for mixing them up):

SachaG commented 8 years ago

Actually i's not that easy, because as far as I can tell there is no way to get a session ID from within SSR code… See https://github.com/kadirahq/flow-router/issues/640

sungwoncho commented 8 years ago

@boxofrox I made a new repo with a reproduction of the error. See here. I am no longer using FlowRouter SSR at the moment, but I can help if there is a need.

SachaG commented 8 years ago

@sungwoncho thanks! btw, what are you using? React Router? Or regular FlowRouter?

sungwoncho commented 8 years ago

@SachaG I am using the regular FlowRouter.

boxofrox commented 8 years ago

Thanks for the repo example. If I have time this evening, I'll dig through it, otherwise I'll start tomorrow.

SachaG commented 8 years ago

FWIW I decided to implement a simpler version of this package myself. This is what I ended up with:

https://github.com/meteor-utilities/react-list-container/blob/master/lib/cursorcounts.js

I couldn't figure out a way to uniquely identify a user so I used another approach: I'm uniquely identifying the cursor instead, based on the publication name and subscription terms.