Welcome Guest, Not a member yet? Register   Sign In
Using Session with a lot of AJAX
#1

Hello all, 
I'm migrating an app from CI3 to CI4 and I take the chance to organize better the app. 
I'm wondering about this part of the documentation: 


Unless you’re developing a website with heavy AJAX usage, you can skip this section. If you are, however, and if you’re experiencing performance issues, then this note is exactly what you’re looking for.
Sessions in previous versions of CodeIgniter didn’t implement locking, which meant that two HTTP requests using the same session could run exactly at the same time. To use a more appropriate technical term - requests were non-blocking.
However, non-blocking requests in the context of sessions also means unsafe, because, modifications to session data (or session ID regeneration) in one request can interfere with the execution of a second, concurrent request. This detail was at the root of many issues and the main reason why CodeIgniter 4 has a completely re-written Session library.
Why are we telling you this? Because it is likely that after trying to find the reason for your performance issues, you may conclude that locking is the issue and therefore look into how to remove the locks …
DO NOT DO THAT! Removing locks would be [b]wrong[/b] and it will cause you more problems!
Locking is not the issue, it is a solution. Your issue is that you still have the session open, while you’ve already processed it and therefore no longer need it. So, what you need is to close the session for the current request after you no longer need it.


My app is not a SPA but it uses a lot of ajax calls. So I should follow this advice but I'm not understanding the flow very well. 
I should destroy the session after a single request is done? 
In my session there is logged_in information, if I destroy it, the user will be redirected to the login page.  

Anyone has used the session with ajax and have followed the documentation direction?
Reply
#2

Yikes! The doc is wrong! session_destroy() or $session->destroy() will do exactly that, destroy the session and log out the user. You should call session_write_close() to save the session and release the lock. There's even a paragraph almost at the end of the page that says to use session_write_close():
Quote:Important

Only MySQL and PostgreSQL databases are officially supported, due to lack of advisory locking mechanisms on other platforms. Using sessions without locks can cause all sorts of problems, especially with heavy usage of AJAX, and we will not support such cases. Use session_write_close() after you’ve done processing session data if you’re having performance issues.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 12-08-2021, 06:45 PM by kenjis.)

Yes, I sent a PR to fix it:
https://github.com/codeigniter4/CodeIgniter4/pull/5447
Reply
#4

(12-08-2021, 06:45 PM)kenjis Wrote: Yes, I sent a PR to fix it:
https://github.com/codeigniter4/CodeIgniter4/pull/5447
Thank you Kenjis! Smile
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5
Music 

(12-08-2021, 05:14 PM)includebeer Wrote: Yikes! The doc is wrong! session_destroy() or $session->destroy() will do exactly that, destroy the session and log out the user. You should call session_write_close() to save the session and release the lock. There's even a paragraph almost at the end of the page that says to use session_write_close():
Quote:Important

Only MySQL and PostgreSQL databases are officially supported, due to lack of advisory locking mechanisms on other platforms. Using sessions without locks can cause all sorts of problems, especially with heavy usage of AJAX, and we will not support such cases. Use session_write_close() after you’ve done processing session data if you’re having performance issues.

Hi, I am facing a lot of problems with a project of mine which is using a lot of ajax calls. Very often the session is spontaneously closing, redirecting the user to the login page.
In your post, are you saying to use session_write_close() instead of destroy() ?
Thank you
Reply
#6

(12-11-2021, 05:04 PM)gbwebapps Wrote: In your post, are you saying to use session_write_close() instead of destroy() ?

Yes.
Reply
#7
Thumbs Up 

Thank you
Reply




Theme © iAndrew 2016 - Forum software by © MyBB