A federal judge in New York ruled Wednesday that U.S. President Donald Trump may not legally block Twitter users because doing so violates their rights under the First Amendment of the U.S. Constitution.

The ruling by U.S. District Judge Naomi Reice Buchwald in Manhattan came in response to a lawsuit filed against Trump in July by the Knight First Amendment Institute at Columbia University and several Twitter users.

The First Amendment protects the right to freedom of expression.

Buchwald agreed with the plaintiffs' contention that the discussions arising from Trump's tweets should be considered a public forum. She rejected an argument made by justice department lawyers that Trump's own First Amendment rights allowed him to block people with whom he did not wish to interact.

Trump was a prolific tweeter from his @RealDonaldTrump account even before being elected in 2016 and has since made it an integral and controversial part of his presidency.

Aides reportedly have tried to rein in his tweeting, which often starts early in the morning. But he has remained unfettered and uses Twitter to promote his agenda, announce policy and attack critics, especially the media, as well as the investigation into alleged Russian connections with his campaign.

The U.S. Department of Justice represented the president in the case. In an email to The Associated Press, department spokesperson Kerri Kupec said, "We respectfully disagree with the court's decision and are considering our next steps."​

The Knight Institute and the individual Twitter users claimed in their lawsuit that by blocking users for their views, Trump was shutting them out of discussion in a public forum, violating the First Amendment.

When one Twitter user blocks another, the blocked user cannot respond to the blocker's tweets on the social media platform.

Media reports say among those Trump has blocked are novelists Stephen King and Anne Rice, comedian Rosie O'Donnell, model Chrissy Teigen, actress Marina Sirtis and the military veterans political action committee VoteVets.org.

Last year, Will Fischer, director of government relations for VoteVets.org, told CBC Radio's Out in the Open that he couldn't believe Trump had blocked his organization at first.

The Commander in Chief can block <a href="https://twitter.com/votevets?ref_src=twsrc%5Etfw">@VoteVets</a>, the voice of 500k military veterans and families, but we will NOT be silenced. <a href="https://t.co/SaCN5hKU9R">pic.twitter.com/SaCN5hKU9R</a> —@votevets

"Since then, quite a few other folks including many other of my fellow veterans, have also lifted up the fact that they've been blocked by the president of the United States on Twitter as well," Fischer said, noting that he found the move troubling.

"The commander-in-chief can block @VoteVets, the voice of 500k military veterans and families, but we will NOT be silenced," the organization tweeted in response to being blocked.

The lawsuit also named Trump's social media director, Dan Scavino, as a defendant.

"While we must recognize, and are sensitive to, the president's personal First Amendment rights, he cannot exercise those rights in a way that infringes the corresponding First Amendment rights of those who have criticized him," Buchwald said.

The judge stopped short of directly ordering Trump to unblock users, saying it was not necessary to enter a "legal thicket" involving courts' power over the president.

"Because no government official is above the law and because all government officials are presumed to follow the law once the judiciary has said what the law is, we must assume that the president and Scavino will remedy the blocking we have held to be unconstitutional," she wrote.

Jameel Jaffer, the Knight Institute's executive director, said in a release that his organization was pleased with the court's decision.

"The president's practice of blocking critics on Twitter is pernicious and unconstitutional, and we hope this ruling will bring it to an end," he said.