President Donald Trump cannot block Twitter users for the political views they have expressed, a federal judge in Manhattan ruled on Wednesday.

Blocking users from viewing his Twitter account — a feature offered by the social media platform — is unconstitutional and a violation of the First Amendment, Judge Naomi Reice Buchwald wrote in her ruling.

"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 wrote.

The government had argued that blocked individuals could still access the president’s tweets. The judge agreed but said that even considering the president's First Amendment rights, preventing users from interacting directly with him on Twitter represented a violation of a "real, albeit narrow, slice of speech."

The case had been brought by the Knight First Amendment Institute at Columbia University.

It argued that the @realDonaldTrump Twitter account is a "public forum" under the First Amendment.

The institute represented seven individuals who had been blocked by account.

"We’re pleased with the court’s decision," said Jameel Jaffer, the Knight Institute’s executive director. "The president’s practice of blocking critics on Twitter is pernicious and unconstitutional, and we hope this ruling will bring it to an end."

There was no immediate response to the ruling from the White House.

The lawsuit was filed last July against Trump and two of his aides, White House social media director Dan Scavino and then-White House press secretary Sean Spicer. It argued that Trump's account constituted a public forum under the First Amendment, and that the White House was violating the plaintiffs' First Amendment right to petition their government for redress of grievances.

The plaintiffs were: a university professor in Maryland, a surgery resident in Tennessee, a Washington state songwriter, a New York comedy writer, an author, a legal analyst and a police officer.

Trump, an avid Twitter user, frequently stokes controversy on social media.

RELATED | 'SPYGATE': Trump ramps up attacks on FBI, Russia probe

In the past week, he has repeatedly claimed in multiple tweets that the Justice Department put a "spy" inside his presidential campaign to frame him for "crimes he didn't commit" amid mounting questions over his campaign's possible collusion with Russia.

The tweets prompted a reply from ex-FBI Director James Comey, who was fired by Trump last year. Comey tweeted on Wednesday: "Attacks on the FBI and lying about its work will do lasting damage to our country."