For more than two hours, Elon Musk and Donald Trump bantered on X about national security, energy policy, immigration and more, in a freewheeling conversation that gave the former president a forum to launch personal attacks on his political rivals as both men reveled in their largely shared vision for the country.

The discussion on the social-media platform’s livestreaming service Spaces—which was delayed because of technical problems—began with Musk asking about the assassination attempt on Trump at a rally in Pennsylvania last month. Musk referred to security failures allowing the shooter to get on a nearby roof. “That does seem crazy,” he said.

“Well, they’re going to learn from this,” said Trump, while praising the Secret Service response. “They moved so fast and let me tell you, that took tremendous courage.”

Trump is in a fierce competition with Vice President Kamala Harris , the Democratic nominee, as polls have shown her gaining momentum since President Biden exited the race for the presidency. The Republican former president has been frustrated by the attention Harris’s ascent has received, and he saw his return to X—known as Twitter when he frequently used it during his presidency—as a way to regain some of the spotlight.

X is a vastly bigger platform for Trump, where he had 88.9 million followers Monday night, compared with his own Truth Social network, with 7.53 million followers.

It was hyped by Trump’s team as “the interview of the century,” but the audio-only event felt more like overhearing a telephone call, often with rambling, between two figures who have grown closer as Musk’s politics have shifted to the right. Musk, who endorsed Trump immediately after the assassination attempt, had said before the event that he didn’t want to conduct a typical interview and that he wanted the discussion to be a conversation.

There were few revelations about Trump’s second-term agenda—and Musk struggled at times to get a word in. The tech entrepreneur struck an overall more nuanced tone than Trump on issues, including immigration. Trump was largely in attack mode.

Musk said most people who enter the country “are actually good hardworking people,” but some aren’t. Trump was decidedly more negative, suggesting criminals and “nonproductive” people were flooding the country.

“These are rough people,” Trump said. “These are people we don’t want in our country.”

Trump falsely claimed that “over 20 million people” entered the U.S. having escaped prisons, mental institutions or insane asylums.

He said that undocumented migrants are entering the U.S. at “levels never seen before.” The U.S. recorded about 57,000 illegal crossings in July, The Wall Street Journal reported, down from around 250,000 in December, when they reached an all-time high.

Musk appeared to raise the idea of a potential position on what he called a government efficiency panel looking at spending of taxpayer money.

“I’d be happy to help out on such a commission, ” Musk said.

“I’d love it,” Trump responded.

The Journal reported in May that Trump and Musk had discussed a possible advisory role for the entrepreneur if Trump won a second term.

The former president, whose campaign has been working to define Harris , called her fake. But he also spent time attacking Biden, at times focusing on his former opponent’s record and calling him stupid. Biden dropped out of the race three weeks ago, but Trump is seeking to latch his new rival to the administration’s policies.

“By the way, you think Biden could do this interview? Do you think that Kamala could do this interview?” Trump said.

A Harris spokesman said: “Trump’s entire campaign is in service of people like Elon Musk and himself—self-obsessed rich guys who will sell out the middle class and who cannot run a livestream in the year 2024.”

When the discussion turned to the use of fossil fuels , Trump suggested the issue was far in the future. “I think we have, you know, perhaps hundreds of years left,” he added.

Musk gently pushed back. “My estimate would be, you know, a little more aggressive than that,” he replied, adding later: “If, I don’t know, 50 to 100 years from now, we’re…mostly sustainable, I think that’ll probably be OK.”

The conversation was plagued by technical issues at the start. Some users trying to listen instead saw a message saying: “Details not available.” Others were able to get into a listening room only to quickly receive a message reading, “This Space is not available.” Confusion and jokes raced across the internet, and the malfunction evoked the kind of issues that marred the presidential campaign launch of Florida Gov. Ron DeSantis on the platform last year.

Right before the event started, Musk cited without evidence a “massive DDOS attack on X,” referring to a distributed denial-of-service, the sort of cyberattack that overwhelms a website, crashing it.

“There’s a lot of opposition to people just hearing what President Trump has to say,” Musk said as the delayed event began. Some observers questioned Musk’s explanation, noting the rest of the platform seemed to continue working.

The technical problems represented a setback at a closely watched moment for X, which Musk has been seeking to promote as the go-to online venue for news ahead of the November U.S. presidential election.

It was slated to start at 8 p.m. About 8:35 p.m. it appeared a microphone was turned on as sound could be heard, suggesting the event could begin, but then silence took over. Around 8:42 p.m., Musk started speaking. Just after it began, the event’s listeners crossed one million users, according to the platform’s figures.

Earlier Monday, ahead of the discussion on X’s Spaces, Trump returned to the platform with several posts—his first since last August, when he posted a photo of his mug shot after he surrendered at an Atlanta jail on charges he conspired to overturn his election loss in Georgia.

Also on Monday, European Union official Thierry Breton sent a letter to Musk warning about the bloc’s online-content rules in the context of events including the interview, though the letter referred to a U.S. “presidential candidate” and didn’t name Trump. The European Commission, the bloc’s executive arm, has been investigating X over its handling of illegal content and disinformation, and Breton in the letter said Monday’s interview is of interest to EU officials because users in the EU can watch it.

X Chief Executive Linda Yaccarino responded on X, posting : “This is an unprecedented attempt to stretch a law intended to apply in Europe to political activities in the US. It also patronizes European citizens, suggesting they are incapable of listening to a conversation and drawing their own conclusions.”

Musk said during the event that America needed safe cities, secure borders and deregulation, pitching a second Trump term as vital to achieving those goals.

“I think really it’s essential that you win for the good of the country,” Musk said. “I’m just stating my opinion.”

Write to Vivian Salama at vivian.salama@wsj.com , Alexa Corse at alexa.corse@wsj.com and Alex Leary at alex.leary@wsj.com