Realtime in your Function Stacks
Learn how to utilize the new Realtime Event function
Last updated
Learn how to utilize the new Realtime Event function
Last updated
While Realtime is fully functional without implementing anything in your function stacks, you may find yourself wanting to build function stacks to extend the functionality of Realtime.
This is possible with the new Realtime Event function, located under APIs & Lambdas in the function panel.
This function sends a message of type 'event' to the channel specified. Remember, a message can be anything from plain text to a JSON object for even further flexibility.
Channel - The channel to send the event to
Data - The payload of the event
Database - If this channel requires authentication, select the corresponding database that handles your authentication here
You can use variables for Channel and Data to make the event behave dynamically.
Please note that Event is different than Message, and will need to be handled accordingly by your frontend.
Realtime connections do not log message history. This means that once a user leaves our Marvel chat room, if they come back, they won't be able to see any of the previous messages. So, we want to store our messages in a database table as they are sent to the channel.
We could approach this in a couple of different ways.
Have our frontend send an API request at the same time a message is sent to the channel to log the message.
Have our frontend only send an API request, and our API can handle delivering the message once it is stored.
For this example, we will use the second option. We need to first modify our frontend code to send the API request, instead of sending the message directly to the channel. We'll do this by defining a new function and then calling it when our button is clicked.
We need to set up a database table to log the messages.
Here's the API endpoint we are sending these requests to.
This endpoint takes in the channel name and message data, fires our Realtime Event function, and then stores the message in the database table.
At this point, we have modified our frontend code and set up the API in Xano to handle the requests. Now, when our users send messages, they will be logged in the database table, and we still get all of the benefits of utilizing the realtime connection.