Connecting Our First WebSocket
Explore how to create and connect your first WebSocket in Phoenix. This lesson guides you through setting up a project, installing dependencies, and running the Phoenix server, enabling you to build foundational real-time application components.
We'll cover the following...
Create a WebSocket connection
To get up and running quickly, we’re going to leverage Phoenix’s initial project scaffold. This course will let us write and run code on the go, but if we want to follow along locally, this is a good time to go to the Appendix lesson in order to make sure that Elixir and Phoenix are set up properly on our system.
We’ll use mix phx.new to create our first example. We’ll be prompted to fetch and install dependencies during this process. Enter Y in order for the project to be started without manual steps.
Run mix phx.server in the hello_sockets folder to start the server. If we get an error when starting the server, we should double-check that we are in the right folder and do not already have a program running on port 4000.
Once started, we’ll see the program running on port 4000:
Steps to create hello_sockets application
-
Connect to the terminal by clicking on the below screen.
-
Run the following command in the terminal given below and compare the results with the above code snippet labeled “Creating new phoenix project.”
-
Enter
Ytofetch and install dependenciesduring this process and wait for the dependencies to be installed completely. -
Go to the
hello_socketsdirectory by entering:
- Run the Phoenix server by entering the following command and verify the result from the above-given code snippet with the caption “Running phoenix server”.