These are chat archives for synrc/n2o

21st
Oct 2016
Matti Katila
@mudyc
Oct 21 2016 06:17
Ah, in cowboy the handler is cowboy_rest and you have rest_cowboy :)
The rest dependency is not part of the sample generated by mad.
But anyway, learning learning learning...
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:03
Hi there
a question here : does the websocket ping/pong mechanism maintain the session active ?
and what is the duration of a session otherwise ? by session here I mean the wf:user() and wf:session() variables maintained server-side.
Namdak Tonpa
@5HT
Oct 21 2016 09:04
N2O uses application level PING/PONG protocol
because at the moment of releasing not all browsers supporting WS PING/PONG
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:06
How to use application level PING/PONG ? do I have to code it myself ?
Namdak Tonpa
@5HT
Oct 21 2016 09:07
session variables expirations from memory are controlled by TTL config value
https://github.com/synrc/n2o/blob/master/src/handlers/n2o_session.erl#L73
@seb3s for sure no
websocket and application level pings are fully automatic
by default JavaScript client send pings every 4s
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:10
yep I see that for the 4 sec websocket ping
Does this means that as long as I keep my web page opened in the browser, the session is maintained active on the server ?
Namdak Tonpa
@5HT
Oct 21 2016 09:11
sure
you have stable erlang process until you stop sending pings
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:11
I seems to me that sometimes I'm asked to login again ? server lost my wf:user() content.
I will double check to be sure of what happens here
thanks for your info
Namdak Tonpa
@5HT
Oct 21 2016 09:12
you mean n2o/sample or your own app?
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:12
my own app
Namdak Tonpa
@5HT
Oct 21 2016 09:12
not supported )
until you publish it to github
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:12
trying to switch from legacy nitrogen to n2o :-)
Namdak Tonpa
@5HT
Oct 21 2016 09:13
cool! :+1:
in case you have problems, just give me access to git repo
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 09:13
ok thanks
AlexanderT
@AlexanderKai
Oct 21 2016 09:59
@seb3s
As I know, by default n2o expires session's values in 15 minutes.
https://github.com/synrc/n2o/blob/master/src/handlers/n2o_session.erl#L73
After 15 minutes session's value will lost.
https://github.com/synrc/n2o/blob/master/src/handlers/n2o_session.erl#L122
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 10:13
yep the last point seems strange to me
as long as I do not touch the session var, if it gets expired, it is removed, wether I have done actions elsewhere in the app
I would expect all sessions vars to expire at the same time, when nothing is done for more than ttl() time, including ping/pong heartbeat.
this is not the case right now as far as i understand the code
Namdak Tonpa
@5HT
Oct 21 2016 10:17
you can write you own cache policy
n2o cache policy used in production since 2011
it is carefully designed and revisited by the community
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 10:18
yes I just want to understand the behaviour to use it correctly
Namdak Tonpa
@5HT
Oct 21 2016 10:18
the behavior is just like in redis
each object has its own living time, and thus expiration time = creation time + ttl
ping interval is completely another tuning value
it is for tracking connection lifetime
not the cached object lifetime
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 10:22
ok, to summarize, if I want to keep my user logged in the system using wf:user() functions, I need to update its value in a laps of time less than ttl(), is that right ?
Namdak Tonpa
@5HT
Oct 21 2016 10:25
you want to wf:user to be never expired?
or you want to expire user (ETS cookies) on different TTL than caching ETS ?
if you need different TTL value this means another config parameter
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 10:28
isn't wf:user() stored as regular wf:session(), just using a predifined "user" key?
Namdak Tonpa
@5HT
Oct 21 2016 10:28
wf:user is wf:session backed by "cookies" ETS
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 10:29
I've got a login page where is set wf:user(my_user). then in other pages, I check whether wf:user() is undefined or not, when it is undefined, I redirect to my login page.
Namdak Tonpa
@5HT
Oct 21 2016 10:29
wf:cache is backed by "caching" ETS
you need to custom PING protocol to update wf:user TTL
or put infinity TTL
usually (in backing systems)
we use application message tracking
we discard user only if it generates no application traffic for 5 minutes
and this could be handled only by custom protocol policy
for n2o/sample to be as simple as possible we use global TTL value for both caching and cookies
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 10:40
Ok thanks a lot Namdak, I'll try to fully understand what this means, and see how to proceed :-)
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 12:52
Back again
I've made some experiments, and there are still a few things I don't catch.
The session_id itself (as defined by the <<"auth">> ets entry) has a ttl that sets an expiration limit.
every session var that I store later on with session(myvar, value) has also an expiration date which is further in time (same ttl but calculated later on)
however when session_id ttl expired, there is a cleaning of all session vars that belongs to that session (seems normal to me) but that means that the session(myvar) ttl will never be reached
Maybe that 'not very clear so let's get an example.
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 12:57
I've decreased the ttl to 120 sec to explorer use cases quickly
I open My login page which sets the session_id cookie and <<"auth">> which expires 2 mins later
I just wait 90 sec on the page and then logon
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 13:03
then 30 secs later, on the destination page, I am logged out because the session_id expires and cleans up my wf_user() data
so my question there is when is the ttl expiration date updated on session_id ? (the one with {cookie, <<"auth">>} )
If it is never updated, I don't catch the point of having different ttl for the successive entries that are also attached on the session. I must be missing something :-(
AlexanderT
@AlexanderKai
Oct 21 2016 13:51
After 30secs did you refresh page?
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 13:52
yep
and I get redirected to the login page because the {sid, "auth"} has expired and therefore cleans my {sid, "user"} entry before its expiration
xiufan
@xiufan
Oct 21 2016 13:56
How to handle browser does not support websocket in n2o?
AlexanderT
@AlexanderKai
Oct 21 2016 13:58
Session value have a definite time to live. Time to live is never updating.
There is two way to get around this issue, in my opinion.
Set expiration interval to infinity, or in PING function update ttl.
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 13:59
when you do a set_value(key, value) the ttl is pushed to a new limit
ets:insert replace the old entry by a new one that caculates a new ttl
Namdak Tonpa
@5HT
Oct 21 2016 14:00
@xiufan use XHR polling js plugin
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:00
but I don't see anywhere in the code that {sid, "auth"} ttl is updated
xiufan
@xiufan
Oct 21 2016 14:01
is it automatically switched?
Namdak Tonpa
@5HT
Oct 21 2016 14:02
it should
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:02
maybe I should just set a big ttl but I would really prefer to understand what I am doing wrong
AlexanderT
@AlexanderKai
Oct 21 2016 14:02
If you set_value, you don't get_value correct.
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:03
yep, but you can set the same value more than once and so updating the ttl
Namdak Tonpa
@5HT
Oct 21 2016 14:04
I think we just need two separate TTL for sessions (cookies ETS) and caches (caching ETS)
the N2O will not update wf:user context on any event
you should do it yourself in app (on set of events)
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:05
yes
Namdak Tonpa
@5HT
Oct 21 2016 14:05
this could be done by plugging your protocol
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:05
i agree with that
you could replace almost any part of N2O
with protocols
e.g. you can plug myapp_heart protocol taht will update TTL on each PING
or you can replace nitrogen protocol
or session protocol
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:06
but to my test even if I do that, it seems that my session data will be expired by the {sid, "auth"} ttl expiration
Namdak Tonpa
@5HT
Oct 21 2016 14:07
which is not protocol but rather nitrogen style request handler
you will just update TTL on any domain message
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:07
do you mean I should also update the ttl for the {sid, "auth"} cookie ets entry ?
Namdak Tonpa
@5HT
Oct 21 2016 14:07
yes
ensure_sid
replace n2o_session or protocol, your choice
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:08
ah ok I thought it was too deep n2o and that I should not touch it :-)
Namdak Tonpa
@5HT
Oct 21 2016 14:08
n2o is 1K LOC
you can touch anything
there is no LOW level in N2O
N2O is abstract protocol router
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:09
yeah and that's why I digging in !!!
Namdak Tonpa
@5HT
Oct 21 2016 14:09
nitrogen is just one protocol
you can use N2O for pure binary data streaming without Nitrogen Elements and pickle/flush messages
Sébastien Saint-Sevin
@seb3s
Oct 21 2016 14:09
good, I will change it to my needs so, thanks again
xiufan
@xiufan
Oct 21 2016 14:11
@5HT where can i find the code about XHR automatically switched
xhr.js in HTML and thats it
Andy
@m-2k
Oct 21 2016 15:16
А нельзя сделать чтобы при реконнекте ws процесс падал по таймауту? Чтобы переподключение было с тем же стейтом
Namdak Tonpa
@5HT
Oct 21 2016 22:50
можно
все можно
это ж эрланг :-)