Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js socket.setRecvBufferSize() Method
Next article icon

Node.js socket.ref() Method

Last Updated : 06 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The socket.ref() method is an inbuilt application programming interface of class Socket within dgram module which is used to get the reference of the particular socket containing all the information in it.

Syntax:

const socket.ref()

Parameters: This method does not accept any parameters.

Return Value: This method returns the reference of the particular socket containing all the information in it.

Example 1: In this example, we will see the use of socket.ref() method.

Filename: index.js

Javascript

// Node.js program to demonstrate the
// server.ref() method
 
// Importing dgram module
const dgram = require('dgram');
 
// Creating and initializing client
// and server socket
let client = dgram.createSocket("udp4");
let server = dgram.createSocket("udp4");
 
// Catching the message event
server.on("message", function (msg) {
 
    // Displaying the client message
    process.stdout.write("UDP String: " + msg + "\n");
 
    // Exiting process
    process.exit();
})
    // Binding server with port
    .bind(1234, () => {
 
        // Getting the reference of the server
        // by using ref() method
        const size = server.ref();
 
        // Display the result
        console.log(size.eventNames());
 
    });
                      
                       

Output:

[ 'message' ] UDP String: Hello

Example 2: In this example, we will see the use of socket.ref() method.

Filename: index.js

JavaScript

// Node.js program to demonstrate the
// server.ref() method
 
// Importing dgram module
const dgram = require('dgram');
 
// Creating and initializing client
// and server socket
let client = dgram.createSocket("udp4");
let server = dgram.createSocket("udp4");
 
// Catching the message event
server.on("message", function (msg) {
 
    // Displaying the client message
    process.stdout.write("UDP String: " + msg + "\n");
 
    // Exiting process
    process.exit();
});
 
// Catching the listening event
server.on('listening', () => {
 
    // Getting address information
    // for the server
    const address = server.address();
 
    // display the result
    console.log(`server listening
        ${address.address}: $ { address.port }`);
});
 
// Binding server with port address
// by using bind() method
server.bind(1234, () => {
 
    // Getting the reference of server
    // by using ref() method
    const size = server.ref();
 
    // Display the result
    console.log(size.eventNames());
});
 
// Client sending message to server
client.send("Hello", 0, 7, 1234, "localhost");
                      
                       

Output:

server listening 0.0.0.0:1234 [ 'message', 'listening' ] UDP String: Hello

Run the index.js file using the following command:

node index.js

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/dgram.html#dgram_socket_ref



Next Article
Node.js socket.setRecvBufferSize() Method

R

RohitPrasad3
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • Node.js-Methods

Similar Reads

  • Node.js socket.address() Method
    The socket.address() method is an inbuilt application programming interface of class Socket within dgram module which is used to get the object which contains the address information for the socket. Syntax: const socket.address() Parameters: This method does not accept any parameter. Return Value: T
    2 min read
  • Node.js socket.addMembership() Method
    The socket.addMembership() method is an inbuilt application programming interface of class Socket within dgram module which is used to make the kernel join a multicast group at the particular multicast address. Syntax: const socket.addMembership(multicastAddress[, multicastInterface]) Parameters: Th
    2 min read
  • Node.js socket.bind() Method
    The socket.bind() method is an inbuilt application programming interface of class Socket within dgram module which is used to bind the particular data gram server to a particular port with some required information. Syntax: const socket.bind(options[, callback]) Parameters: This method takes the fol
    2 min read
  • Node.js socket.connect() Method
    The socket.connect() method is an inbuilt application programming interface of class Socket within dgram module which is used to connect the particular server to a particular port and address. Syntax: const socket.connect(port[, address][, callback]) Parameters: This method accepts the following par
    3 min read
  • Node.js socket.dropMembership() Method
    The socket.dropMembership() method is an inbuilt application programming interface of class Socket within dgram module which is used to make kernel leave a multicast group at the particular multicast address. Syntax: const socket.dropMembership(multicastAddress[, multicastInterface]) Parameters: Thi
    2 min read
  • Node.js socket.getSendBufferSize() Method
    The socket.getSendBufferSize() method is an inbuilt application programming interface of class Socket within dgram module which is used to get the size of the socket to send buffer in bytes. Syntax: const socket.getSendBufferSize() Parameters: This method does not accept any parameters. Return Value
    2 min read
  • Node.js socket.getRecvBufferSize() Method
    The socket.getRecvBufferSize() is an inbuilt application programming interface of class Socket within dgram module which is used to get the size of socket receive buffer in bytes. Syntax: const socket.getRecvBufferSize() Parameters: This method does not accept any parameter. Return Value: This metho
    2 min read
  • Node.js socket.setTTL() Method
    The socket.setTTL() method is an inbuilt application programming interface of class Socket within dgram module which is used to set or clear the IP_TTL socket option which helps to specify how many IP hops that a packet is allowed to travel through the specified router or gateway. Syntax: const sock
    2 min read
  • Node.js socket.setMulticastTTL() Method
    The socket.setMulticastTTL() is an inbuilt application programming interface of class Socket within dgram module which is used to set or clear the IP_MULTICAST_TTL socket option which helps to specify how many numbers of IP hops that a packet is allowed to travel through the specified multicast traf
    2 min read
  • Node.js socket.unref() Method
    The socket.unref() method is an inbuilt application programming interface of class Socket within dgram module which is used to allow the process to exit even if the socket is still listening. Syntax: const socket.unref() Parameters: This method does not accept any parameters. Return Value: This meth
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences