Code structure
Last updated
Last updated
In this guide you will learn how InstaQuiz client works and how you can extend it for your own usage.
The client uses Vue.Js and Tailwindcss for a good UI experience for the users, axios and Socket.IO client library for communicating with the server.
Vue's state management library is used to manage state around the application each page has it's own "store" that manages state of the page. stores are at ./client/src/stores
.
Communication with Telegram is done through their library for web apps that is injected in the index.html file located in ./client/index.html
. you can add below line to your index file's header and use it in your own app.
It attaches a Telegram object to window property, in this project ./client/src/types/telegram.d.ts
was created to handle types for all variables and functions in the Telegram object. and has detailed explanation about all of them and is up to date with Bot API 6.9 alternatively you can install package for automatically receiving updates to the API with updating it, run below command to install:
Telegram passes user's main theme colors thorugh css variables and you can use them in your project. in this project it was configured with Tailwindcss's extended themes.
you can use them throughout the web app with tailwind syntax for example a background color will be:
There 3 main pages in the application:
HomeView
JoinView
GameView
Game id for joining is passed through bot's link and start_param query, in router's beforeEach route guard when home page is opened we check if start_param is present and forward the user to JoinView
To make the code more readable views have been separated in some components in the ./client/src/components
folder and they are prefixed with the name of the view.
The ones without a view prefix are used by multiple views.
./client/src/configs
files are for global functions and tools used in the application and it contains code for axios base setup of handling default base url of http server, headers and handling errors which if it was an auth 401 error it will try to close the web app.
And socketConfigs.ts
for configuring the connection with the servers socket.
Each emit request can also have a callback that when the code on the server is done returns a result which is more like a Rest API that can be useful for knowing the result of the operation or receive some extra data.
For the callback to work on the server, the client has to have a callback function as it's last inputted parameter.
An example for a callback in the code:
Each view can listen to events coming from the socket and update the ui, for example the wait list:
Remember to use computed values only for UI components, functions in the store do not need reactivity.
One more colorConfigs.ts file is there to get a random tailwindcss color string to use when creating the circle beside the user's name in leaderboard, because when starting the app with menu button or an inline button the user's field will not be sent.
Almost all elements in the application are simple HTML one's except the category selector for the HomeView that is located in ./client/src/components/HomeSelect.vue that uses 's ListBox elements for a feel of still being in Telegram.
When each view is mounted we can call which will tell Telegram that the app is ready to be shown to the user and also .expand() function to expand the page and be able to show more data.
Each client can listens to updates regarding the game they're in with the gameId that was sent from them to the server which joins them in a Socket.IO which is used to assign messages sent from the server to the correct client.
All reactive elements in the web app are handled by VueJs's .
was used for holding reactive data, and for processing new data based on the reactive objects. an example of computed: