fafhrd91 / pyramid_sockjs

44 stars 13 forks source link

Can I .send() something from a regular Pyramid view? #20

Closed thapar closed 11 years ago

thapar commented 11 years ago

Is there any way to .send() something to the current user from a regular Pyramid view?

fafhrd91 commented 11 years ago

you have to know session id for current user to send messages to it. pyramid_sockjs does not now anything about user id. well, there is a way, but it is very unoptimal. poramid_sockjs maintain internal session key as (authenticated_userid(request), sid), so you can do something like:

   for key, session in session_manager.items():
        if key[0] == current_user_id:
            return session

but i strongly suggest to add custom mapping between user id and session.

thapar commented 11 years ago

I have the session id mapped to the username in a dictionary, username as the key. So if I'm in a regular pyramid view, not a sockjs request/session, how can I .send() to the session id? Or how can I get ahold of the sockjs session? On Jan 22, 2013 12:52 PM, "Nikolay Kim" notifications@github.com wrote:

you have to know session id for current user to send messages to it. pyramid_sockjs does not now anything about user id. well, there is a way, but it is very unoptimal. poramid_sockjs maintain internal session key as (authenticated_userid(request), sid), so you can do something like:

for key, session in session_manager.items(): if key[0] == current_user_id: return session

but i strongly suggest to add custom mapping between user id and session.

— Reply to this email directly or view it on GitHubhttps://github.com/fafhrd91/pyramid_sockjs/issues/20#issuecomment-12556030.

fafhrd91 commented 11 years ago

session manage holds all active session objects. you can use request.get_sockjs_manager(name), name is a name of sockjs route, by default it is empty string

so code can look like this:

   def my_view(request):
       sm = request.get_sockjs_manager()
       cur_sid = {'you user mapping'[userid]}
       sm[cur_sid].send('message')
thapar commented 11 years ago

request.get_sockjs_manager() gives me {('Guest6752', u'8z5hjq25'): <newsite.views.chat.ChatSession object at 0x5093810>}, where 'Guest6752' is the user's authenticated username on the website. So three questions:

1) Is ('Guest6752', u'8z5hjq25') the expected key? 2) Also, why is cur_sid a set? 3) By using the userid/username, how does that allow me to differentiate which sockjs Session the request came from (so I don't have to sm[cur_sid].send('message') to all the user's active sessions)?

thapar commented 11 years ago

Mr. Nikolay?

fafhrd91 commented 11 years ago

session manager uses (userid, sessionid) for key, this is additional security for non persistent transports, like polling or streaming. You can disable this behavior, use config.add_sockjs_route(...., per_user=False)

thapar commented 11 years ago

And for questions 2 and 3, my good sir?

fafhrd91 commented 11 years ago

2) what is cur_sid i dont see this in pyramid_sockjs code 3) re-state the question, i dont understand

thapar commented 11 years ago

2) in your example above ( https://github.com/fafhrd91/pyramid_sockjs/issues/20#issuecomment-12559918 ) you wrote cur_sid = {'you user mapping'[userid]}, perhaps as a typo, otherwise it looks like you are creating a set.

3) if a user has multiple tabs open, how can I .send() to the tab that sent the request. Using your example above would retrieve the full list of sessions for a username, and I would have to .send() to all the sessions. Instead, I would like to .send() only to the tab that sent the original request. Again, I'm looking to use .send() from a regular Pyramid view.

fafhrd91 commented 11 years ago

2) just example, you can put there anything

3)

    class CustomSession(Session):

        def on_message(self, msg):
              # do processing
              ...

              self.send('message')
thapar commented 11 years ago

3) that example does not use a regular Pyramid view, where request is used instead of Session. Your original example ( https://github.com/fafhrd91/pyramid_sockjs/issues/20#issuecomment-12559918 ) was for a regular Pyramid view.

fafhrd91 commented 11 years ago

you have to develop some method how to map pyramid request and sockjs session. pyramid_sockjs does not do this.

thapar commented 11 years ago

Can you suggest on where I can start sniffing to figure this out?

fafhrd91 commented 11 years ago

thats depends on flow of your application. i dont see reasons for such mapping at all because pyramid view is invoked before sockjs session even beeing created.

fafhrd91 commented 11 years ago

if you have questions please use my email.