Skip to content

English documentation

Diego Martínez Sánchez edited this page May 6, 2023 · 1 revision

Websockets

This project has 2 main microservices which are one is an API in which you can manage the topics and messages queue. There are a lot of improvements that can be made but the focus of this project is to have the websocket working as expected.

Actions

The actions that can be performed are the next ones:

Subscribe to public topics.

{"action": "suscribe","topic_name": "public_topic_to_suscribe"}

Subscribe users to private topics in which another user is part of.

{"action": "invite","topic_name": "private_topic_name_to_invite","username": "eder"}

To send messages to a topic or to a user in specific. In the topic name should be the user if its the case you want to send a directly message.

{"action": "message","topic_name": "noticias","content": "Content of the message"}

Disconnect from the server in a normal way.

{"action": "disconnect"}

Also something else that can be consider as an indirect action is the functionality to send pending messages if for example a user that is part of a topic but one or more users are not connected then a message is stored in the database so when user is connected again then they receive the pending messages again.

Good practices

In order to follow a kind of standard for good programming practices I implemented a pylint as a github action to be run every time a push is made to the develop branch, this will help us to correct and follow good practices.

Considerations

The code has is been developed in python 3.10, so this code will only run in versions of python 3.10 onwards.

Sometimes in windows it cant be run scripts like the same wscat, so in order to be able to execute the wscat we should execute the next command as administrator in the powershell.

To be able to run wscat in powershell:

Set-ExecutionPolicy RemoteSigned

And to go back to normality:

Set-ExecutionPolicy Restricted

About authentication for the websocket server there is a endpoint that retrieves the same token to be used, as query params you should include yes or yes the user so the API can generate the token:

So username word should be exactly the username that is going to be used to add to the connected users in the websocket API. After you get the token below the way to use it to authenticate to the websocket API.

Connection and authentication

Below we can see some users created with the purpose to test the present exercise so in this case we are going to use wscat which is a nodejs dependency that can be installed through npm, this means also that you should have nodejs, you can click here to go to the official page of nodejs and install it if its the case you want to test with wscat, so follow the instructions in that link. After nodejs is isntalled in your respective operative system then you should install wscat by running the next command below:

npm install -g wscat

For more information about wscat you can click here to go to the documentation of wscat library.

Now commands below can be used to authenticate to the websocket server, in this specific case then we use a script part of this project which is gen_tokens.py to generate an updated token, it expires in one day, of course better jwt can be performed but for this case we only need to comply that the jwt is still current. Below after the port and the / is the token per user.

omar

wscat -c ws://localhost:5000/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODI4Mjc1OTUsImlhdCI6MTY4Mjc0MTE5NSwic3ViIjoib21hciIsInBheWxvYWQiOnsiZGVzY3JpcHRpb24iOiJBIHRva2VuIHRvIGF1dGhlbnRpY2F0ZSB0byB3ZWJzb2NrZXQgc2VydmVyIn19.J0edkFnsJ3_Vj3jCfBOA08JzPLN8CpQSOXVv5a6Xf-U

diego

wscat -c ws://localhost:5000/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODI4Mjc0MjgsImlhdCI6MTY4Mjc0MTAyOCwic3ViIjoiZGllZ28iLCJwYXlsb2FkIjp7ImRlc2NyaXB0aW9uIjoiQSB0b2tlbiB0byBhdXRoZW50aWNhdGUgdG8gd2Vic29ja2V0IHNlcnZlciJ9fQ.MzdeAK7lC3kZC8QZd0Q4WdnaqLvQlMUCHLuMUaIFghk

satoshi

wscat -c ws://localhost:5000/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODIzNDkwNTcsImlhdCI6MTY4MjI2MjY1Nywic3ViIjoic2F0b3NoaSIsInBheWxvYWQiOnsiZGVzY3JpcHRpb24iOiJUZXN0IHRva2VuIn19.Mxl6LujulMFeGWF7LvNG5G4NbFwp18HAQKoV7LgmhVM

memoor

wscat -c ws://localhost:5000/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODIzNDkxMTksImlhdCI6MTY4MjI2MjcxOSwic3ViIjoibWVtb29yIiwicGF5bG9hZCI6eyJkZXNjcmlwdGlvbiI6IlRlc3QgdG9rZW4ifX0.DNO5TU4ie8-gPWUx9BEtai1QD_3MyZg7tlDE7kRdcII

About deployment

In this case i took into consideration that the labor of deployment should be part of devops, As you can see inside the folders api and websocket there is inside a Dockerfile which is responsability of the same developer cause there you need to copy the files necessary to run the script and also to install all dependencies needed to run correctly the microservice. However, the deployment part should be configured by a devops or SRE area and using the Dockerfiles created by the developer, so thats why I dont remember how to do it cause in my actual jobs that part is fully dedicated to a devops or SRE team. But of course, if its needed I can learn.

Unit tests

About the API was easier to create unit tests cause as a backend developer I create unit test all the time that I create a new API. In this case the unit tests for the API runs in the github action designed for this. If you want to run locally the unit tests then you should go inside the api folder and run the next command:

python -m unittest api_test

For the websocket I wasnt able to create the unit tests because one, I haven't created tests for a websocket before so I'll have to do some more research on how to Mock the answer of the websocket and also the API responses inside the websocket and write so I have to break myself into several parts to fulfill the responsibilities of my current job and also the times when I should rest, otherwise I would be overloaded if I only code all the time, so I apologize for this case, but eventually I will upload them more calmly and having more time because I dont like to left projects incompleted when are at this point of development.

API

This API is used by the same websocket service but also some of the endpoints here should be used from the client before going to the websocket server. The postman collection as an example can be found by going to the side bar and clicking to the postman collection option.

Wiki

The project

Clone this wiki locally