Basic template for any node

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node

class MyCustomNode(Node): # MODIFY NAME
    def __init__(self):
        super().__init__("node_name") # MODIFY NAME

def main(args=None):
    rclpy.init(args=args)
    node = MyCustomNode() # MODIFY NAME
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ == "__main__":
    main()

How to setup publisher and subscriber

Create WS --> create package

Inside src/<pkg_name>/<pkg_name> create publisher/subscriber.py node

For a topic choose suitable interface

ros2 interface show example_interfaces/msg/Int64

int64 data

for detailed list of ROS2 interfaces

ros2 interface list

want to list all packages that provide interfaces

ros2 interface packages

Import that interface in node

from example_interfaces.msg import Int64
from std_msgs.msg import Char

Create the publisher in the constructor using create_publisher() method

self.number_publisher_ = self.create_publisher(Int64,    #interface type
                                               "number", #topic name 
                                               10        #queue size
                                               )

robotics is to do X action every Y seconds. —> create a timer in construct

self.number_timer_ = self.create_timer(1.0, 
                                       self.publish_number  #callback function
                                       )