I have a table where the data comes from an API call. This table is used by multiple users, and each user can select only one row. However, I want to ensure that once a user selects a row, it becomes unavailable to other users—it should either be hidden from them or they should be unable to select it.
My approach is to use WebSocket for real-time communication since I am using APIs.
@Arslan_Doddamani,
Are you asking for help on how to configure the front end so that table rows are unclickable or disappear when another user clicks it? Or are you asking for help on how to set up web sockets?
If it's the web sockets part, here are a couple resources for how to incorporate web sockets into your retool app:
Thank you @lindakwoo
I was asking for help on how to configure the front end so that table rows are unclickable or disappear when another user clicks it.
Web sockets was my approach to solve it. If there is any other approach then I am happy to know it.
Add a js query to listen for incoming websockets. When one comes in, trigger the query that gets the data for the table. Go to the advanced tab in this js query and click on: "Run this query on page load"
@Arslan_Doddamani,
If you are not querying a database, but instead are using an api, that is fine. Instead of querying the database, make a call to the api and then add a transformer to transform the data to remove the items where lockedAt is not equal to null (unless you want to add another api endpoint to retrieve unlocked rows only). Then use this transformed data as the source data for your table.
The table will be the same as before, but instead of triggering a change to the database, you'll trigger the api call that locks the row and then automatically call the api that gets the table when that succeeds. To pass the row id, you can trigger this api with the same additionalScope object that I describe above in triggering the change to the database. And then when you listen for the websocket, you will trigger the api that gets the table data.
Current Output:
The selected row becomes invisible.
So far, everything is working fine.
Now, my task is to implement real-time row selection.
If you've ever used Google Sheets with a team, you'll notice that when someone selects a cell, it's locked for others to select or edit. I want to implement the same functionality—when one user selects a row, others shouldn't be able to select it.
This is where you implement a js query that runs on page load that listens for web sockets. When it receives a web socket message, it will then trigger a refetching of api that gets the table data.
So in real time, when one user selects a row, that action will trigger the lock row api call and then a fetching of the table data, and then it will also send a web socket message. All other users will immediately receive that web socket message, which will then trigger then to make the API call which will return the latest data.
I want to implement the functionality—when one user selects a row, others shouldn't be able to select it . when user select other row then it should selected row should be unselected.
I wanted to know some other things too.
Where the web sockets is used in this we are just calling the api's. if server emits something it will not display until we reload or call a api.
When I implemented this in react.js I had to use the useEffect to have a connection with bi-directional connection.
The table has pagination and will require lot of time to fetch the data. So reloading of the page will be not efficient.
The user will select the row for just 2 to 3 minutes.
Hi @Arslan_Doddamani,
Sorry, but I was mistaken. You cannot listen for a websocket in the regular retool app. You must create a custom component and set up your useEffect in it like you have done above. Then you import it into your parent app and pass data (the specific row information from the websocket message) from the custom component to the main parent app.
In order to not have to fetch the api every single time, in the parent app, set up a global variable from your initial api fetch that captures the table data. Set this global variable as your data source for the table. When you receive the new data from the custom component, after it receives the websocket message, you can update this global variable to set whatever row needs to be locked.
In the table itself, you can filter the rows by adding a filter that checks if the "Locked" column is true or false: