Media playback is unsupported on your device Media caption A look at the problems facing Labour in Oldham

At Oldham's annual end-of-summer carnival you see the town's 20th-Century history in flesh and blood.

The crowds mingle in the sunshine, kids queue for ice-cream, adults for fish and chips; young Asian women in saris or headscarves enjoy the fairground rides and shirtless men with dogs on chains watch performers on temporary stages.

It is a tradition that dates back to Oldham's days of industrial greatness. Before World War One, the town produced more cotton than France and Germany combined. By the 1960s and 70s, more than 300 cotton mills working day and night had pulled in immigrant workers from across the Asian sub-continent.

"This was a brilliant place for a young immigrant in the 1970s," Riaz Ahmad told me. He came here from Pakistan in 1974 at the age of 21, and has been a Labour councillor for much of the last 25 years.

"The town was full of noise and smoke from the chimneys. The mayoress's chain of office has 365 diamonds - one for each of the cotton mills that operated here."

When the world was buying cotton, Oldham thrived, a genuine beneficiary of a global market. But then the world started making cotton, and much more cheaply; Oldham's decline was as sudden as it was dramatic. The tide of globalisation had turned. This year Oldham was named, by the Office for National Statistics, the most deprived town in England.

The townscape is still dominated by those famous old mills. But many are derelict, their windows smashed, their roofs stripped of copper and lead, water leaking into what was their immense production halls.

Image caption The ONS named Oldham the most deprived town in England

Throughout, Oldham remained a Labour fortress. Though Winston Churchill was once the town's Liberal MP, Labour have been winning elections here hands down for as long as anyone can remember. But in June, Oldham, like most of post-industrial England, voted decisively to leave the EU, in defiance of Labour's pro-Remain stance.

"I'd always voted Labour," Marlene Nurse, a retired schoolteacher told me. "But then we got a leaflet through the door from UKIP. We didn't know anything about that party so we went along to their public meeting. [My husband Ian and I] met Paul Nuttall [UKIP deputy leader] there. It was an eye-opener. He was talking common sense.

"We'd never heard anything like it from the Labour Party or the Conservatives. I thought - this is what we want. From then on we were committed."

In 2005, UKIP polled less than 3% in Oldham; in a parliamentary by-election this year in Oldham West and Royton, it polled 23% - propelling the party into second place.

Oldham has drawn a new wave of migrants since EU enlargement in 2004.

Image caption Riaz Ahmad says Labour is not complacent about the challenge it faces in Oldham

"Lots of east Europeans have been sent here because the housing is cheap and the rents are low," Marlene Nurse told me. "The media try to say UKIP supporters are racist, but it's not true. We are ordinary, decent, honourable people.

"Nigel Farage gets a terrible time in the media, he's barely allowed to finish his sentence before the interviewers cut him off. But if you actually listen to what he says, he makes sense - common sense."

"I am not complacent about this threat," says Riaz Ahmad. "We will not sleepwalk into that. We are working in the estates to hold onto our support. But UKIP are a one-agenda party, and that issue is now resolved [by the EU referendum].

"So what has UKIP got to offer? They'll have to reinvent themselves, so we'll see what they come up with."

The EU referendum result revealed a profound divide in British society. It is not the traditional divide, between Conservative and Labour, but one that cuts through both major UK parties.

It is as though the Britain that thrived under globalisation and open markets, the Britain that voted to stay in the EU, failed to notice that another Britain had been incubating for decades in the dereliction to which the once proud industrial heartlands have been reduced since the 1970s.

An entrenched hostility to the EU has been building in that second Britain in direct proportion to the decline of its industrial base.

The vote on 23 June was, in Oldham and towns like it, a revolt against globalisation and a revolt against open markets. But how much was it also a revolt by people who have always voted Labour?

Real votes in real elections are still going Labour's way in Oldham. But the EU referendum has shifted something in the alignment of political loyalties in the old industrial heartlands.

Anti-EU and anti-Westminster sentiment emerges from it emboldened. Labour still has the numbers; but UKIP has the energy, the momentum, and a renewed sense of its own legitimacy as the authentic voice of those left behind in a globalised economy,

"If I were a Labour MP or councillor here in Oldham, should I be scared?" I asked Marlene Nurse. "Oh I hope so," she said, and laughed. "I certainly hope so!"