Updated April 3, 2023
Introduction to Socket in Ruby
Socket in Ruby is a way to send or communicate to one network services to another network services. we have used api for communication but in case of socket it allow us to communicate bidirectional. Here bidirectional communication means both the network can send and receive on the same connection , because of that the changes which reflect after communication no need for another call or refresh (in more technical way it allow us to develop real time applications). To implement socket we have many different channels like unix domain socket , UDP ,TCP etc , for example you have seen the stock exchange site where changes happens automatically without refreshing the page which means communications are happening and reflecting is any changes will be there.
Types of Socket in Ruby
As we have already discussed that we have various types of ways to use the socket , on this basis we can have three types of the socket in Ruby they are given below.
1. TCP Socket
TCP socket is part of the socket module of Ruby .If we wanted to create the socket using TCP then we can use the TCPSocket class .Here for these classes we need to provide the destination IP address and the port number of destination socket. With help of this we will get the connection. In case if the connection failed or get some error while connecting it will display the Errno::ECONNREFUSED.
require "socket"
TCPSocket.new hostname port
2. Unix Socket
UNIX bases sockets are made for use on the same system , here you can refer same system to your own computer system which allow us to communicate with various process available on the same system .UNIX based sockets are faster as they are on the same system so no need for checking of routing .Unix socket is part of the socket module of Ruby .If we wanted to create the socket using a UNIX domain stream client then we can use the UNIXSocket class of Ruby socket module. Here we need to pass path of process on the same host to the UNIXSocket for communication .
require "socket"
UNIXSocket.new path
3. UDP Socket
First point to be noted here is that UDP is connectionless.UDP socket is part of the UDP socket module of Ruby .If we wanted to create the socket using UDP then we can use the UDPSocket class of socket module. Here we need to pass the address family to the UDPSocket for communication. For more details we can read about variou address families available for UNIX socket. Addresses family should be integer value .
require "socket"
UDPSocket.new [address_family]
How Socket works in Ruby?
To explain the working of socket in Ruby we are going to take reference of below diagram with help of below steps .
- In the below diagram we can see that many clients are connecting to the server and sending and receiving the response on the same connection. Because of this we called this connection as the bi-directional connection as both parties can send and receive the data.
- Every client will create an instance of the server like serverInstance = TCPSocket.open(host, port-number) and can read whatever data sent by the server.
- Second on the server side it will open applications on the specific port server = TCPServer.open(port-number). Here port-number will be any integer value like 3000 ,3002 etc . Same port number will be used on the client side to communicate .
- A real time example for socket ,if you are using facebook then you must have noticed that if there is any notification on facebook you are not required to refresh the page to reflect the notification. So they have their own socket system which automatically pushes data to all the connected clients from the server if there is any change for that client .
- Server can send data to all connected client and also can receive from the connected clients .
Please follow the below diagram for better understanding:
Examples to Implement Socket in Ruby
To explain one example of socket in Ruby we are going to create two files one is for server side and another is for client side.
Let me first explain the server side code with help of the below steps:
- We have included the socket module .
- Next we have created or opened a server on the port 2000 , we can use any other port to operate the server.
- Nex on the created instance of connection we are waiting for any client for connection .
- Once there will be any client connected with this server using the same port which we have defined then the server will send some data.
- Here we are sending Date and after sending the date we are notifying the client about the closing of the socket.
See the below example of file socketServer.rb and screen where we have executed this file .
File socketServer.rb,
require 'socket" # Including the socket module
serverSocket = TCPServer.open(2000) #Here we are opening socket on the port 2000
loop {
clientSocket = serverSocket.accept # Connect to the client
clientSocket.puts(Time.now.ctime) # Sending some data to the client
clientSocket.puts "We have done with the sending data to the client, now closing socket"
clientSocket.close # Close the socket
}
Output:
Once the server is on we need some client to connect with the server to get the data sent by the server. In the below example we are creating an instance of the server with the same port 2000 on which the server opened. Once we will execute the below file we will get the output sends by the server .
Please see the below example of code along with the screen of output.
File clientSocket.rb
require 'socket' # Including the socket standard library of Ruby
hostName = 'localhost'
portNumber = 2000
socketServer = TCPSocket.open(hostName, portNumber)
while line = socketServer.gets # Here we are reading line coming from the socket
puts line.chop # Printing data
end
socketServer.close # We need to close the socket which we have open
Output:
Conclusion
From these tutorials we learned some important uses and basic concepts of socket in Ruby with the help of one useful and real example , we also learned the working of socket with the help of diagram and little syntax , here we have given little focus on the uses of socket in real world use cases also.
Recommended Articles
We hope that this EDUCBA information on “Socket in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.