Build Your First Ruby Chat App: A Step-by-Step Guide

Creating a chat application allows users to communicate with each other in real-time. In this tutorial, we will walk you through building a basic Ruby chat app using sockets. By the end of this guide, you'll have a working chat application that you can run on your local machine.

Prerequisites

Before we begin, ensure that you have the following installed on your system:

  1. Ruby: You can download and install Ruby from the official website.
  2. A text editor: You can use any text editor of your choice like Sublime Text, Atom, or Visual Studio Code.

Step 1: Creating the Server

The server is responsible for managing client connections and broadcasting messages to all connected clients. Create a new file called server.rb and add the following code:

require 'socket'

server = TCPServer.new('localhost', 3000)
clients = []

loop do
  Thread.start(server.accept) do |client|
    clients << client
    username = client.gets.chomp
    puts "#{username} has joined the chat."

    client.puts "Welcome to the chat, #{username}!"

    loop do
      message = client.gets.chomp
      break if message == 'exit'

      puts "#{username}: #{message}"
      clients.each { |c| c.puts "#{username}: #{message}" unless c == client }
    end

    puts "#{username} has left the chat."
    clients.delete(client)
    client.close
  end
end

This code sets up a TCP server on localhost using port 3000. It listens for incoming client connections and creates a new thread for each client. The server stores all connected clients in an array and broadcasts messages to all clients, except the sender.

Step 2: Creating the Client

The client is responsible for connecting to the server, sending messages, and displaying received messages. Create a new file called client.rb and add the following code:

require 'socket'

print 'Enter your username: '
username = gets.chomp

client = TCPSocket.new('localhost', 3000)
client.puts(username)

Thread.new do
  loop do
    message = client.gets.chomp
    puts message
  end
end

loop do
  message = gets.chomp
  break if message == 'exit'

  client.puts(message)
end

client.close

This code connects to the server on localhost using port 3000. It asks the user for their username and sends it to the server. The client then creates a new thread to listen for incoming messages from the server and displays them to the user.

Step 3: Running the Chat Application

To run the chat application, first, start the server by executing the following command in your terminal:

ruby server.rb

Next, open a new terminal window and start the first client:

ruby client.rb

Finally, open another terminal window and start the second client:

ruby client.rb

You can now send messages between clients, and the server will broadcast them to all connected clients.

Conclusion

You have successfully created a simple Ruby chat application using sockets. This is just the beginning; you can further enhance this chat application by adding features like authentication, private messaging, and more. Happy coding!

An AI coworker, not just a copilot

View VelocityAI