OTTAWA -- U.S. President Barack Obama brought the House down Wednesday as he passed a diplomatic torch of sorts to Canadian counterpart Justin Trudeau, along with a pointed warning about the perils of injustice and inequality.

In what was almost certainly his last visit to Canada as president, Obama held court for nearly an hour before a joint session of the House of Commons and the Senate, basking in cascades of applause and heaping praise on the country's young new prime minister, with whom he's become fast friends.

Trudeau has brought new energy and leadership to Canada and to the everlasting alliance between the two countries, Obama said. The world will continue to benefit from Trudeau's time in office, he added, as his own era comes to an end.

But even as he lauded his host country for championing values like integrity, human rights and fair play, he urged it to pay its full share to NATO -- a solitary note of discord in a speech focused on-- Canada-U.S. harmony.

"As your ally and as your friend, let me say that we'll be more secure when every NATO member, including Canada, contributes its full share to our common security," Obama said.

Canada has committed to meeting a standard target of spending two per cent of its GDP on defence, but has been lagging well below that mark for several years.

"The world needs more Canada, NATO needs more Canada. We need you."

That direct request sets the stage for the next time Trudeau and Obama are likely to cross paths -- next month's NATO summit in Poland.

The world finds itself at a pivotal moment, when the international order the two countries helped build together is under strain, he continued.

"The world is more connected than ever before, but even as it spreads knowledge and the possibility of greater understanding between peoples, it also empowers terrorists who spread hatred and death."

Obama referenced the deadly shooting in Orlando that killed 49 people earlier this month, as well as the terrorist attack at the airport in Istanbul on Tuesday that killed 42 people and injured scores of others.

And he warned that with globalization and technology comes a commensurate increase in inequality and wage stagnation.

"If our democracies seem incapable of assuring broad-based growth and opportunity for everyone, then people will push back out of anger or out of fear," Obama said.

"Politicians -- some sincere and some entirely cynical -- will tap that anger and fear, hearkening back to bygone days of order and predictability and national glory, arguing that we must rebuild walls and disengage from a chaotic world, or rid ourselves of the supposed ills brought on by immigrants, all in order to regain control of our lives."

Trudeau introduced Obama with words of praise and a nod to the president's final months in office.

"History books will record signature policies, (but) what I will remember -- what I hope we all will remember -- are the lessons you taught us not by executive order, but by example."

Obama's visit to the Hill saw parts of Ottawa locked down for hours, with roads and waterways closed to traffic and hundreds of police officers deployed through the downtown core, even to a local deli where there was rumour Obama might make an appearance.

Many bystanders got caught in the historic moment by accident, trapped by roads suddenly closed by a passing motorcade. But others took the trip to Parliament Hill to watch the speech broadcast on giant TV screens.

"At some point, somewhere, your family was an outsider," Obama said. "And so the mothers, the fathers, the children we see today, they're us. We can't forsake them."

Toronto resident Tamara Blitz-Miller, her mother from what is now Lithuania and father from Poland, said that part of the speech resonated with her.

"I think that's something that Canadians value and I hope to teach my kids that lesson, even though I'm not sure they were listening today."

Obama's speech was full of the requisite jokes about hockey, Canadian celebrities taking over Hollywood, Canada's weather and the perils of high office -- his hair has turned so grey, he said, his wife calls it the "Great White North."

"We can all agree that our democracies are far from perfect, they can be messy and they can be flawed, and they can leave all sides of a debate unsatisfied," he said.

"Justin is just getting started."

Both countries, he said, must do more to live up to the ideals they offer and fight back when responses to legitimate fears result in some people being turned into scapegoats.

"We have to call this mentality what it is: a threat to the values we profess, the values we seek to defend."

Obama was greeted by a sustained standing ovation and cheers as he entered the House of Commons, shaking hands with cabinet ministers as he took his seat. The ovations continued to thunder around him throughout his speech, extending it to nearly an hour.

While on the floor of the House of Commons was jammed with MPs, senators and their staff, the galleries above were equally packed with a roster of Canadian celebrities, business leaders and other dignitaries.

Obama's remarks bookend his relationship with Canada in general and Ottawa in particular -- it was the scene of his first presidential visit outside the U.S. in 2009, not long after he was elected the previous year.

Wednesday's speech was also the first in the House of Commons by a U.S. president since Bill Clinton in 1995.

-- with files from Joanna Smith