Index: next.config.js
===================================================================
--- next.config.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ next.config.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -2,4 +2,10 @@
 const nextConfig = {
   reactStrictMode: true,
+  
+  webpack: (config) => {
+    config.experiments = config.experiments || {}
+    config.experiments.topLevelAwait = true
+    return config
+  },
 }
 
Index: pages/api/poker/gameStates.js
===================================================================
--- pages/api/poker/gameStates.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/poker/gameStates.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -1,8 +1,10 @@
-export let tables = []
+import { tables } from "../postgre/index";
+
+import { v4 as uuidv4 } from "uuid";
 
 export const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
-                    "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
-                    "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
-                    "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
+                           "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
+                           "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
+                           "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
 
 export const deck = [...singleDeck];
@@ -17,4 +19,5 @@
     round: 0,
     turnIdx: -1,
+    lastActivity: 0,
     pot: 0,
     lastBet: 20,
@@ -45,2 +48,182 @@
     },
 }
+
+
+/**
+ * ********************* BEGIN OF FUNCTIONS *********************
+ */
+
+export function createTable(playerId, playerName, tableName) {
+    const tableId = uuidv4();
+
+    const table = {
+        id: tableId,
+        name: tableName,
+        status: '_1_just_created',
+        creator: playerName,
+        started: false,
+        ended: false,
+        round: 0,
+        turnIdx: -1,
+        lastActivity: 0,
+        prevTurnIdx: -2,
+        pot: 0,
+        lastBet: 20,
+        turnsSinceLastBet: 0,
+        deck: [...deck],
+        players: [{
+            id: playerId,
+            table: tableId,
+            credits: 0,
+            status: '_1_just_entered',
+            displayName: playerName,
+            cards: [],
+            betAmount: 0,
+            wonAmount: 0,
+            isSatDown: false,
+            isCoordinator: true,
+            isFolded: false,
+            isGhost: false,
+            hand: {
+                hand: '',
+                highCard: 0,
+            },
+        }],
+        onlyOnePlayerLeft: false,
+        winners: [],
+        splitWinners: false,
+        cards: [],
+    }
+
+    tables.push(table)
+
+    return table;
+}
+
+export function getRestrictedTablesArray() {
+    let result = [];
+
+    tables.forEach(table => {
+        let tmpPlayers = [];
+        table.players.forEach(player => {
+            tmpPlayers.push({
+                ...player,
+                id: '',
+                table: '',
+                cards: '',
+            })
+        });
+
+        let tmpWinners = [];
+        table.winners.forEach(winner => {
+            tmpWinners.push({
+                ...winner,
+                id: '',
+                table: '',
+                cards: '',
+            })
+        });
+
+        let tmp = {
+            ...table,
+            deck: [],
+            players: tmpPlayers,
+            winners: tmpWinners,
+            turnTimeout: null,
+        }
+
+        result.push({...tmp});
+    })
+
+    return result;
+}
+
+export function getRestrictedTableArray(tableId, session_id) {
+    let result = undefined;
+
+    let tableIdx = tables.map(e=>e.id).indexOf(tableId);
+
+    if (tableIdx !== -1) {
+        let table = tables[tableIdx];
+
+        let tmpPlayers = [];
+        table.players.forEach(player => {
+            if (player.id === session_id) {
+                tmpPlayers.push({
+                    ...player,
+                    id: '',
+                    table: '',
+                })
+            }
+            else {
+                tmpPlayers.push({
+                    ...player,
+                    id: '',
+                    table: '',
+                    cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
+                })
+            }
+        });
+
+        let tmpWinners = [];
+        table.winners.forEach(winner => {
+            if (winner.id === session_id) {
+                tmpWinners.push({
+                    ...winner,
+                    id: '',
+                    table: '',
+                })
+            }
+            else {
+                tmpWinners.push({
+                    ...winner,
+                    id: '',
+                    table: '',
+                    cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
+                })
+            }
+        });
+        result = {
+            ...table,
+            players: tmpPlayers,
+            winners: tmpWinners,
+            turnTimeout: null,
+        }
+    }
+
+    return result;
+}
+
+export function getTable(tableId) {
+    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
+
+    if (tableIdx !== -1) {
+        return tables[tableIdx];
+    }
+
+    return undefined;
+}
+
+export function getTableAndPlayer(session_id) {
+    for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
+        const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
+
+        if (playerIdx !== -1) {
+            return {
+                success: true,
+                table: tables[tableIdx],
+                player: tables[tableIdx].players[playerIdx],
+            }
+        }
+    }
+
+    return {
+        success: false,
+        table: {...sampleTable},
+        player: {...samplePlayer},
+    };
+}
+
+/**
+ * ********************* END OF FUNCTIONS *********************
+ */
Index: pages/api/poker/index.js
===================================================================
--- pages/api/poker/index.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/poker/index.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -3,186 +3,9 @@
 require('dotenv').config();
 
-import { v4 as uuidv4 } from 'uuid';
-
-import { tables, deck, sampleTable, samplePlayer } from './gameStates'
-
-import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded } from './tableSpecific'
-
-/**
- * ********************* BEGIN OF FUNCTIONS *********************
- */
-
-function createTable(playerId, playerName, tableName) {
-    const tableId = uuidv4();
-
-    const table = {
-        id: tableId,
-        name: tableName,
-        status: '_1_just_created',
-        creator: playerName,
-        started: false,
-        ended: false,
-        round: 0,
-        turnIdx: -1,
-        pot: 0,
-        lastBet: 20,
-        turnsSinceLastBet: 0,
-        deck: [...deck],
-        players: [{
-            id: playerId,
-            table: tableId,
-            credits: 0,
-            status: '_1_just_entered',
-            displayName: playerName,
-            cards: [],
-            betAmount: 0,
-            wonAmount: 0,
-            isSatDown: false,
-            isCoordinator: true,
-            isFolded: false,
-            isGhost: false,
-            hand: {
-                hand: '',
-                highCard: 0,
-            },
-        }],
-        onlyOnePlayerLeft: false,
-        winners: [],
-        splitWinners: false,
-        cards: [],
-    }
-
-    tables.push(table)
-
-    return table;
-}
-
-function getRestrictedTablesArray() {
-    let result = [];
-
-    tables.forEach(table => {
-        let tmpPlayers = [];
-        table.players.forEach(player => {
-            tmpPlayers.push({
-                ...player,
-                id: '',
-                table: '',
-                cards: '',
-            })
-        });
-
-        let tmpWinners = [];
-        table.winners.forEach(winner => {
-            tmpWinners.push({
-                ...winner,
-                id: '',
-                table: '',
-                cards: '',
-            })
-        });
-
-        let tmp = {
-            ...table,
-            deck: [],
-            players: tmpPlayers,
-            winners: tmpWinners,
-            turnTimeout: null,
-        }
-
-        result.push({...tmp});
-    })
-
-    return result;
-}
-
-function getRestrictedTableArray(tableId, session_id) {
-    let result = undefined;
-
-    let tableIdx = tables.map(e=>e.id).indexOf(tableId);
-
-    if (tableIdx !== -1) {
-        let table = tables[tableIdx];
-
-        let tmpPlayers = [];
-        table.players.forEach(player => {
-            if (player.id === session_id) {
-                tmpPlayers.push({
-                    ...player,
-                    id: '',
-                    table: '',
-                })
-            }
-            else {
-                tmpPlayers.push({
-                    ...player,
-                    id: '',
-                    table: '',
-                    cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
-                })
-            }
-        });
-
-        let tmpWinners = [];
-        table.winners.forEach(winner => {
-            if (winner.id === session_id) {
-                tmpWinners.push({
-                    ...winner,
-                    id: '',
-                    table: '',
-                })
-            }
-            else {
-                tmpWinners.push({
-                    ...winner,
-                    id: '',
-                    table: '',
-                    cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
-                })
-            }
-        });
-        result = {
-            ...table,
-            players: tmpPlayers,
-            winners: tmpWinners,
-            turnTimeout: null,
-        }
-    }
-
-    return result;
-}
-
-function getTable(tableId) {
-    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
-
-    if (tableIdx !== -1) {
-        return tables[tableIdx];
-    }
-
-    return undefined;
-}
-
-function getTableAndPlayer(session_id) {
-    for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
-        const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
-
-        if (playerIdx !== -1) {
-            return {
-                success: true,
-                table: tables[tableIdx],
-                player: tables[tableIdx].players[playerIdx],
-            }
-        }
-    }
-
-    return {
-        success: false,
-        table: {...sampleTable},
-        player: {...samplePlayer},
-    };
-}
-
-/**
- * ********************* END OF FUNCTIONS *********************
- */
+import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
+
+import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded, progressRoundTillTheEnd } from './tableSpecific'
+
+import { tables, cleanTables, update_tables_to_database, load_tables_from_database } from '../postgre/index'
 
 /**
@@ -266,8 +89,11 @@
 
                 if (okayToGo) {
+                    table.lastActivity = Date.now();
                     setNextPlayerIdx(table.id);
                 }
             }
             
+            update_tables_to_database();
+
             res.end();
         }
@@ -291,4 +117,5 @@
                 })
 
+                table.lastActivity = Date.now();
                 table.started = true;
                 table.round = 1;
@@ -309,4 +136,6 @@
                 })
             }
+
+            update_tables_to_database();
             
             res.end();
@@ -327,4 +156,6 @@
             }
 
+            update_tables_to_database();
+
             res.end();
         }
@@ -340,4 +171,6 @@
 
             if (success) {
+                table.lastActivity = Date.now();
+                
                 player.isGhost = true;
                 player.isFolded = true;
@@ -352,4 +185,6 @@
                 }
             }
+
+            update_tables_to_database();
 
             res.end();
@@ -394,4 +229,6 @@
             }
 
+            update_tables_to_database();
+
             res.end();
         }
@@ -412,4 +249,6 @@
             }
 
+            update_tables_to_database();
+
             res.end();
         }
@@ -425,4 +264,16 @@
 
             const { success, table, player } = getTableAndPlayer(session_id);
+
+            if (table.started && !table.ended) {
+                const d = Date.now();
+
+                if (d - table.lastActivity > 30000) {
+                    if (table.players[table.turnIdx] !== undefined) {
+                        table.players[table.turnIdx].isFolded = true;
+
+                        setNextPlayerIdx(table.id);
+                    }
+                }
+            }
 
             res.json({
Index: pages/api/poker/tableSpecific.js
===================================================================
--- pages/api/poker/tableSpecific.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/poker/tableSpecific.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -1,3 +1,5 @@
-import { tables, deck } from './gameStates'
+import { tables } from '../postgre/index';
+
+import { deck } from './gameStates'
 
 import { hands, getBestHandDetails } from './handEvaluations';
@@ -62,5 +64,5 @@
     const tableIdx = tables.map(e=>e.id).indexOf(tableId);
 
-    if (tables[tableIdx] !== undefined && !tables[tableIdx].ended) {
+    if (tables[tableIdx] !== undefined && tables[tableIdx].started && !tables[tableIdx].ended) {
         const table = tables[tableIdx];
 
@@ -80,4 +82,5 @@
 
         if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+        table.prevTurnIdx = -2;
 
         let counter = 10;
@@ -91,15 +94,4 @@
             if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx].isSatDown && !table.players[table.turnIdx].isFolded) {
                 if (table.round >= 2 && table.players[table.turnIdx].credits === 0) continue;
-
-                let prevTurnIdx = table.turnIdx;
-                table.turnTimeout = setTimeout(() => {
-                    if (prevTurnIdx === table.turnIdx) {
-                        if (table.players[table.turnIdx] !== undefined) {
-                            table.players[table.turnIdx].isFolded = true;
-                            
-                            setNextPlayerIdx(table.id);
-                        }
-                    }
-                }, 30000);
 
                 table.lastBet = getMaxBet(table.id) - table.players[table.turnIdx].betAmount;
@@ -151,4 +143,5 @@
         table.round = 0;
         table.turnIdx = -1;
+        table.lastActivity = 0;
         table.turnTimeout = null;
         table.pot = 0;
@@ -233,4 +226,6 @@
         table.started = false;
         table.ended = true;
+        if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+        table.turnTimeout = null;
 
         table.onlyOnePlayerLeft = true;
@@ -301,4 +296,6 @@
         table.started = false;
         table.ended = true;
+        if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+        table.turnTimeout = null;
         if (table.ended && table.winners.length === 0) {
             setWinner(table.id);
@@ -327,4 +324,6 @@
                 table.started = false;
                 table.ended = true;
+                if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+                table.turnTimeout = null;
             }
 
Index: pages/api/postgre/index.js
===================================================================
--- pages/api/postgre/index.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/postgre/index.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -6,4 +6,6 @@
 
 const crypto = require('crypto');
+
+import { progressRoundTillTheEnd } from '../poker/tableSpecific';
 
 const Pool = require('pg').Pool
@@ -11,7 +13,4 @@
   connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DB}`
 });
-
-const sessions = []
-// example session = { id, displayName, username, credits, lastActivity }
 
 export default function handler(req, res) {
@@ -97,4 +96,6 @@
           });
         }
+
+        update_sessions_to_database();
           
         res.json({
@@ -159,4 +160,6 @@
           }
         });
+
+        update_sessions_to_database();
 
         res.json({
@@ -253,4 +256,6 @@
 
         axios.get(`${process.env.HOME_URL}/api/blackjack/?action=remove_room&session_id=${session_id}`);
+
+        update_sessions_to_database();
       }
 
@@ -427,6 +432,8 @@
                       lastActivity: Date.now(),
                     }
-                    
+
                     sessions.push(session);
+
+                    update_sessions_to_database();
     
                     res.json({
@@ -453,2 +460,59 @@
   }
 }
+
+
+/**
+ * User session data
+ */
+export var sessions = []
+
+export function update_sessions_to_database() {
+   pool.query('UPDATE sessions SET data = $1 WHERE identifier = $2', [JSON.stringify(sessions), 'sessions_data'], (error, results) => {
+     if (error) throw error;
+   });
+}
+   
+export function load_sessions_from_database() {
+   pool.query('SELECT data FROM sessions WHERE identifier = $1', ['sessions_data'], (error, results) => {
+     if (error) throw error;
+ 
+     sessions = JSON.parse(results?.rows[0]?.data || []);
+   });
+}
+load_sessions_from_database();
+ 
+ /**
+  * Poker game data
+  */
+export var tables = []
+ 
+export function cleanTables() {
+   tables = [];
+}
+ 
+export function update_tables_to_database() {
+   tables = tables.map(table => ({...table, turnTimeout: null}));
+ 
+   pool.query('UPDATE poker SET data = $1 WHERE identifier = $2', [JSON.stringify(tables), 'poker_data'], (error, results) => {
+     if (error) throw error;
+   });
+}
+   
+export async function load_tables_from_database() {
+   pool.query('SELECT data FROM poker WHERE identifier = $1', ['poker_data'], (error, results) => {
+       if (error) throw error;
+ 
+       tables = JSON.parse(results?.rows[0]?.data || []);
+ 
+       tables.forEach(table => {
+         if (table.started) {
+           progressRoundTillTheEnd(table.id);
+         }
+       })
+ 
+       cleanTables();
+ 
+       update_tables_to_database();
+   });
+}
+load_tables_from_database();
