最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - meteor app: how to get a unique sessionclient id without using accounts - Stack Overflow

matteradmin7PV0评论

In my meteor app, I need to be able to capture some form of unique element (session id/client id, etc...) to help keep track of the actions that user makes. At this point, I'm not using accounts package, so I'm just looking for a way to capture some sort of client-unique data element, which I could use in the data model to track activities/steps of every unique user. What application / browser / session element could I use in place of this unique id string?

In my meteor app, I need to be able to capture some form of unique element (session id/client id, etc...) to help keep track of the actions that user makes. At this point, I'm not using accounts package, so I'm just looking for a way to capture some sort of client-unique data element, which I could use in the data model to track activities/steps of every unique user. What application / browser / session element could I use in place of this unique id string?

Share Improve this question asked Apr 8, 2014 at 13:41 Eugene GoldbergEugene Goldberg 15.6k23 gold badges98 silver badges179 bronze badges 1
  • You should take a look at answers from that question – Remi Commented Apr 8, 2014 at 13:57
Add a ment  | 

3 Answers 3

Reset to default 6

This what you are looking for?

You can just use Random.id() to get a unique id string. I have used this a number of times to track temporary objects and it is super helpful. There are other Random methods too that may e in handy.

What you can try to use is DDP connection's session ID Meteor.connection._lastSessionId. But be careful, because it is basically websocket session. So when the client open a new tab or refresh the page, this sessionId will be different .

If you want to keep the same session in the browser you can try to implement your own localStorage based session.

Some thoughts: Instead of using Meteor's Random.id(), insert a new doc into a Mongo collection, this way you are guaranteed there will be no collisions. As there is always a tiny chance that Random.id() will return a duplicate. Is that a correct assumption?

Meteor docs say likely to be unique, i.e. Not guaranteed to be unique. http://docs.meteor./#/full/random

Store the _id of the doc in a Session.set() call, to use as your anonymous userId, i.e. Session.get("anonUserId") When done you can delete it to keep the collection trim.

Post a comment

comment list (0)

  1. No comments so far