JiwoonKim / Flack

Online Chatting Web Service similar to Slack (Web50 - project 2)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flack ๐Ÿ’ฌ

Practicing JavaScript, ajax and websockets in particular, to run single page application.

Ajax์™€ ์›น์†Œ์ผ“์„ ์ค‘์‹ฌ์œผ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์—ฐ์Šตํ•˜๊ธฐ ์œ„ํ•œ ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ.

๐Ÿ’ฌ Online Messaging Service

  • For CS50 Web Programming with Python and JavaScript - project 2.
  • Created an online messaging service similiar to Slack.
  • Used Python and Flask as the server framework.
/signin /newchannel
user can sign in user can create new channel
/channel
user can send text messages in real-time within a channel
  • Utilized different methods of connnection between client and server:
    1. In general, GET & POST requests and responses (defined in application.py).
      • the javascript function for creating a new channel is not ajax (even though the action is defined in javascript, it is the same as creating a form in html; it submits a form via POST request and then the server sends a response to render the entire page. The function is merely defined in javascript because the form would have to encompass a large portion in the channel.html file if it were to be in the html file.)
    2. Websockets for posting new messages in order for persistent connection (because making request everytime is inefficient) and broadcasting messages to all users.
    3. Ajax for changing channels so that the sidebar remains the same while only the main content part is modified. (Websockets have problem with blockages and ajax is simplier for rest apis)
  • For the purpose of focusing on practicing javascript, channels and messages are stored as global variables instead of in databases. For this reason, they will be initialized every time the server restarts.

๐Ÿ’ฌ ์˜จ๋ผ์ธ ๋ฉ”์„ธ์ง€ ์„œ๋น„์Šค

  • ํ•˜๋ฒ„๋“œ์˜ CS50 MOOC(edX)์˜ Web Programming with Python and JavaScript ์ˆ˜์—…์˜ ํ”„๋กœ์ ํŠธ 2.
  • Slack๊ณผ ์œ ์‚ฌํ•œ ์˜จ๋ผ์ธ ๋ฉ”์„ธ์ง€ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœ.
  • Python๊ณผ Flask๋ฅผ ์„œ๋ฒ„ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์‚ฌ์šฉํ•จ.
  • ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„ ๋‹ค์–‘ํ•œ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์„ ์‚ฌ์šฉํ•จ:
    1. ์ „๋ฐ˜์ ์œผ๋กœ GET์™€ POST request์™€ Response๋ฅผ ์‚ฌ์šฉํ•จ.
      • ์ƒˆ ์ฑ„๋„์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ajax๊ฐ€ ์•„๋‹Œ post request์— ํ•ด๋‹นํ•จ. (HTML ํŒŒ์ผ์—์„œ Form์„ ์ ์šฉํ•˜๊ธฐ๋ณด๋‹ค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์— ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด์–ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ HTML form์„ ๋งŒ๋“ค์–ด post request๋ฅผ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์„ ์„ ํƒํ•จ)
    2. ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ์›น์†Œ์ผ“์„ ์‚ฌ์šฉํ•จ. (์ง€์†์ ์ธ ์—ฐ๊ฒฐ๊ณผ ๋‹ค์ˆ˜์˜ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐฉ์†ก์„ ๋ณด๋‚ด๋Š”๋ฐ ํšจ์œจ์ )
    3. ์ฑ„๋„์„ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ์ด๋“œ๋ฐ”๋ฅผ ์ œ์™ธํ•œ ์ฝ˜ํ…์ธ ๋งŒ ๋ฐ”๋€Œ๊ธฐ ์œ„ํ•ด Ajax๋ฅผ ์‚ฌ์šฉํ•จ.
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ์ง‘์ค‘ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์ฑ„๋„๊ณผ ๋ฉ”์„ธ์ง€๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์•„๋‹Œ ํŒŒ์ด์ฌ ์„œ๋ฒ„ ๋‚ด์˜ ๋ฒ”์šฉ ๋ณ€์ˆ˜๋กœ ์ •์˜๋˜์–ด ์žˆ์Œ. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„๊ฐ€ ์žฌ์‹œ์ž‘ํ•  ๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”๋จ.

๐ŸŒฑ what I learned | ๋ฌด์—‡์„ ๋ฐฐ์› ๋Š”๊ฐ€

This was a bit challenging project because I had to learn and implement http request-responses, ajax, and websockets all together.

However, I have learned a lot. Particularly, I learned the importance of implementing an efficient workflow and the difference between REST (http request-responses, ajax; half-duplex) and Websockets (full-duplex) and when to implement them.

Now, I am starting to feel limited by Flask and think I should move on to a more sophisticated framework for more complex applications.

  • learned that it is important to create an ordered list of things to do so that the workflow is more efficient and I am not wasting a lot of time contemplating on what I should do next. -> create the ordered list based on the steps the user interface should flow.
    • ex. (login -> access channel page/choose channel -> display messages accordingly -> write message -> post message via ajax -> and so)
  • starting to think maybe it is better to just implement css (using grids and flexboxes) instead of using templates like bootstrap since I have to modify them.
  • learned the importance of css specificity. Since id has more specificity/priority over class, id in parent elements can override class in child elements. So best to use classes in general than ids. If necessary use !important.
  • learned that functions assigned to variables are not hoisted. That is, const name = () => {} must come before it is called.

learned the differences between http requests-responses, ajax, and websockets:

  1. HTTP request-responses is the most general and easiest way to communication between client and server (one-way). The client sends a request (maybe with some data associated) and the server sends back a response. Ususally, a page is sent back and rendered.
    • On the simplest level, the html file and server code consists this kind of communication. However, Javascript may also be used to send a GET or POST request. For example, you can create a form and send it to the server using Javascript but this is does not mean it is ajax (the method is exactly the same with send a form from the html file).
  2. Ajax is an asynchronous method of communication and is coded via Javascript only. The method is the same with http request-responses between client and server (one-way) but just asynchronous. The differences between the two can be found in that ajax only modifies parts of the page instead of the entire page and thus, does not need to refresh to see modifications. Also in the case of ajax, the server usually sends back data (json) instead of a page.
  3. Websockets is a different kind of model which provides a persistent and bi-directional communication (full-duplex communication) and thus, is efficient for real-time communication like messaging and chatting. Especially, websockets are useful for broadcasting data to all users. Websockets can also be used to asynchronously modify the pages.
  • it is okay to use ajax and websockets together. Use them in difference cases where they will be efficiently uses.
  • addEventListener์™€ inline event(in js file) ex) onclick์˜ ์ฐจ์ด์  ์ดํ•ด

  • Quick notes about Flask-SocketIO: flask socketio session is managed differently from flask session. (it forks the Flask user session at the time the client connects to the server over Socket.IO) so any change after socketio is connected is not reflected into socketio's session. It can be thought as having separate sessions. Also emitting is not a necessity (check flask-socketio documentation for details)

About

Online Chatting Web Service similar to Slack (Web50 - project 2)


Languages

Language:HTML 35.4%Language:JavaScript 31.9%Language:Python 17.6%Language:CSS 15.1%