The University of California, Berkeley has cancelled a speech by rightwing internet troll Milo Yiannopoulos after thousands of students gathered in protest and a group of black-clad anti-fascist activists shot fireworks at the speech venue.

The Martin Luther King Jr student union was heavily fortified behind several layers of police barricades when protesters began gathering outside at 5pm on Wednesday, three hours before the event was scheduled to start.

The gathering was boisterous but peaceful until about 6pm, when several dozen protesters wearing black face masks and carrying glittering flags arrived. The group quickly attacked the police barricades, then began shooting firecrackers at the building. Some used barricades to smash windows.

After about 15 minutes, a police officer announced that the event was cancelled.

Some cheered when the police announced the cancellation, but others continued to jeer and call for the police to send Milo out to face the crowd. “Milo isn’t here,” one police officer shouted amid the din. “Milo isn’t here.”

Around 6pm local time, Yiannopoulos posted a statement on Facebook claiming that “the Left is absolutely terrified of free speech and will do literally anything to shut it down”.

After the event was cancelled, and after the crowd watched a light pole that had been set aflame burn itself out, the atmosphere in the crowd quickly turned festive.

A large sound system was turned on, blasting music. The first song played was YG’s Fuck Donald Trump.

At one point riot police on the student union’s second floor appeared to fire non-lethal weapons at the crowd, but it was unclear what kind.

“We won’t put up with the violent rhetoric of Milo, Trump or the fascistic alt-right,” said a Berkeley history student who declined to give his name. The student, who was dressed in black and wore a face mask, carried a banner that read “Queers bash back”. He said he identified with the “antifa” (anti-fascist) movement.

“We are willing to resist by any means necessary,” he added.

Lana Wachowski, a protester who gave a name that is also the name of the writer and director of The Matrix, defended using extreme tactics to deny Yiannopolous a platform. “The moral imperative is to win,” she said. “There’s something to be said for fighting according to a code, but if you lose, people are going to die. People are going to get deported.

“It’s absolutely acceptable to use violence. They are 100% certain to use it against us.”

Trump threatened to pull federal funding from Berkeley in a tweet sent early on Thursday morning:

The Yiannopoulos event, which was sponsored by the Berkeley College Republicans, has divided the former home of the free speech movement in recent weeks. Many students, alumni and community members demanded that action be taken to stop Yiannopoulos from spreading his racist and transphobic views.

Last week, university chancellor Nicholas Dirks defended Yiannopoulos’s right to speak on campus, though he described the Breitbart editor as “a troll and provocateur who uses odious behavior in parts to ‘entertain’, but also to deflect any serious engagement with ideas” whose “act [is] at odds with the values of this campus”.

Dirks also addressed Yiannopoulos’s tendency to single out individual students on stage – such as a transgender student at the University of Wisconsin-Milwaukee – for mockery and abuse, which the chancellor said does “not justify prior restraint on his freedom of expression”.

The Berkeley College Republicans said in a statement that the opportunity to invite Yiannopoulos was “too good to pass up”, though it disclaimed that the group “does not agree with everything Milo has said or done, and totally disavow [sic] any violence or hurt that could result from this event”.

Just hours before the protests, Reddit banned an “alt-right” subreddit page, r/altright, that had become a community for white nationalists. The page now reads: “This subreddit was banned due to a violation of our content policy, specifically, the proliferation of personal and confidential information.”

A fire set by demonstrators on the University of California Berkeley campus. Photograph: Ben Margot/AP

Yiannopoulos is a well-known figure in the rightwing internet, though he was permanently banned from Twitter in July 2016 for instigating racist and sexist abuse of Ghostbusters actor Leslie Jones. Since 2015, Yiannopoulos has traveled to university campuses as part of his “Dangerous Faggot” tour, frequently provoking student protests.

• This article was amended on 2 February 2017. An earlier version referred to the University of Wisconsin Madison where the University of Wisconsin-Milwaukee was meant. Also, a previous version identified a protester as Lana Wachowski, which is likely a pseudonym. Lana Wachowski is one half of the sibling duo that wrote and directed The Matrix