New Jersey police dispatchers could not get a response from Franklin Township patrol Officer Matthew Ellery early one morning in April, so they sent another officer -- who happened to also be a paramedic -- to his last known location.

Ellery’s fellow officer saved his life as he overdosed on heroin in the driver’s seat of the patrol car.

Ellery, 29, of Middlesex, on Friday pleaded guilty to possession of a controlled dangerous substance and driving while intoxicated, according to the Somerset County Prosecutor's Office. As part of his plea, he will apply for admission into a five-year drug court-based treatment program. Failure to complete the progra would result in three to five years in New Jersey State Prison.

>> Read more trending news

Ellery, who NBC News reported had been an officer since August 2016, also agreed to forfeit his position as a police officer. His driver's license will be suspended for seven months.

Somerset County Prosecutor Michael Robertson said in a statement that Ellery was on duty just after 1 a.m. April 7 when dispatchers could not reach him. When his fellow officer found him, he was parked in the driveway of a business. Ellery was unresponsive in the driver's seat of the car.

"The responding officer, who is also an EMT (emergency medical technician), determined that defendant Ellery was experiencing an opiate overdose because he was cyanotic, had no carotid pulse and was not breathing," Robertson's statement read. "The officer removed defendant Ellery from the vehicle and administered two doses of naloxone to him. After receiving the two doses of naloxone and before the officer could commence CPR, defendant Ellery regained consciousness."

Naloxone, also known by the brand name Narcan, is a drug that can rapidly reverse the effects of an overdose of opioids, such as heroin, fentanyl and pain pills like hydrocodone and oxycodone.

According to the National Institute on Drug Abuse, the drug works as an opioid antagonist. It binds to the opioid receptors in a person's brain and blocks or reverses the effects of the drug causing the overdose.

"It can very quickly restore normal respiration to a person whose breathing has slowed or stopped as a result of overdosing with heroin or prescription opioid pain medications," the institute's website says.

Naloxone, which can be injected or administered via a nasal spray, has become a tool carried by police officers, deputies and paramedics across the country as they see a drastic uptick in opioid overdoses. Some states are also training civilians, including librarians, to administer the drug and some pharmacies will sell naloxone over the counter to customers, including chronic pain patients, at risk of an accidental overdose.

Watch the video at the bottom of this story to learn more about how to obtain naloxone. 

The online reaction to Ellery's plea, for which he will be sentenced next month, was mixed on the Facebook page of the county prosecutor's office. Some posters noted that, when naloxone was first made available in the area, comments flooded the page stating that "junkies shouldn't be saved."

"Guess what?" one man wrote. "It's possible to be aggravated at this officer, 'cause he's out arresting people for drugs while on drugs himself, but still have sympathy for him as a human being and what he's going through."

Others offered Ellery prayers and well wishes that he can get the treatment he needs.

"Addiction can happen to anyone, even you, because it does not discriminate," one woman wrote.

Another woman described addiction as a monster that affects everyone in the addict’s family.

"I hope he can get the help he needs. Heroin is a killer," the woman wrote. "This probably saved his life."