Hours before he took to the state Capitol’s Grand Staircase in July 2010 to sound a defiant challenge to Governor Deval Patrick, Speaker of the House Robert A. DeLeo brought a onetime antagonist, state Representative Martin J. Walsh, into his office for a chat.

Walsh, the Dorchester Democrat now running to be mayor of Boston, had backed DeLeo’s rival, John H. Rogers of Norwood, for the speakership the year before. And he had been an occasional thorn in DeLeo’s side since then, part of a rump caucus still disgruntled from what had been a nasty, back- alley leadership fight.

But that day, DeLeo was in a pitched battle with Patrick over crucial details of casino gambling legislation, and he needed Walsh and his ties to labor and his knack for cobbling together coalitions, to be part of a last-ditch bid to shore up enough support to overwhelm Patrick’s veto threat.

For Walsh, locked now in a tight match with City Councilor John R. Connolly, the moment was emblematic of his 16-year career at the State House. Repeatedly, he has angered powerful House speakers and been relegated to the sidelines. And repeatedly, trading on his ability to form alliances with unlikely partners, Walsh has made his way back to semi-prominence.

Get Fast Forward in your inbox:

Forget yesterday's news. Get what you need today in this early-morning email.

As Walsh and his allies have frequently noted during the mayoral campaign, his legislative career extends far beyond labor to human services, human rights, and governmental reforms. He has been a consistent advocate for more funding to combat substance abuse and homelessness. And he has pleased progressive colleagues by legislating against stereotype, standing up early and consistently for gay marriage. He has called his vote to protect gay marriage one of his proudest moments.

But what never truly wavered, despite rough patches over education reform and collective bargaining rights, was Walsh’s alliance with unions.
Walsh has made little secret that he is organized labor’s go-to guy.

Walsh arrived in the House in 1997 from a political hothouse district where he topped a field that included Martha Coakley, then an assistant district attorney. Due to geographic proximity, ethnicity, and style, he was quickly grouped with other, mostly young, Irish-American and largely urban lawmakers who marched under the banner of Speaker Thomas M. Finneran.

But their relationship was not always smooth, a harbinger of Walsh’s later troubles under other speakers.

During his tenure, Walsh has frequently been the chief sponsor of union-friendly legislation, sometimes written in collaboration with unions. The example most frequently cited during this year’s campaign has been an arbitration measure that would strip city councils of the authority to approve or reject arbitration rewards for police and firefighters.

Walsh wrote the bill alongside the Professional Fire Fighters of Massachusetts, and first filed it in 2002. Walsh has said the proposal, which has languished in the House despite being refiled five times, would bolster municipalities’ power to protect finances.

Most of the legislation Walsh has championed on behalf of workers has been more pedestrian. After a string of workers died in floor-finishing accidents, Walsh pushed legislation banning the sale and commercial use of lacquer floor sealers, a measure less about union-building than about protecting low-wage workers in a dangerous industry. Many workers in the industry, both union and nonunion, are Vietnamese men from the Fields Corner section of Walsh’s neighborhood.

Michele McDonald/Globe Staff/file

State Representative Martin J. Walsh was to Senator John F. Kerry’s left during the St. Patrick’s Day roast in 2003.

That bill passed, but several others have gone nowhere, including a bill Walsh filed this year that would have guaranteed county correction officers no lost pay for injuries suffered on the job, a benefit already enjoyed by municipal police officers and firefighters.

Walsh explains that he frequently eschews the bully pulpit and legislating by press conference for a quieter, more collaborative technique.

“I try to get my legislation incorporated in other pieces of legislation,” he said.

Other legislation on which Walsh has worked has been far broader. In 2004, he joined a commission focused on rewriting the state’s public construction project laws. More than three decades after a scandal involving state building contracts at the University of Massachusetts Boston prompted sweeping reforms, both private contractors and trade unions were clamoring for new changes.

Convening powerbrokers across industries and the state government, the commission devised a plan designed to save money in the multibillion-dollar churn of public construction, increase competition, minimize project delays, and improve quality.

“We had every group that naturally fought each other . . . supportive of our legislation,” Walsh said last week during a meeting in his Beacon Hill campaign office. “They had tried blue-ribbon commissions to reform public construction’’ for 30 years, he said, “never were able to get it done, and we were able to get a piece of legislation through the House and Senate unanimously.” The bill became law that summer.

Still, much of Walsh’s State House career has been spent working as a dealmaker with an eye on a broader philosophy, rather than advocating for individual bills. When House members want nonunion projects to go forward without the presence of the infamous, giant inflatable rat used to shame contractors deemed insufficiently prounion, they call Walsh to pacify the building trades.

He has also helped broker electoral support for his colleagues from unions, which can provide financial backing and manpower for campaigns. That ability gives Walsh political clout that he can wield at will.

Perhaps most importantly for Walsh’s standing on Beacon Hill, he has also frequently served as a liaison between House leadership and unions on major legislation that demands concessions from labor. Colleagues and House aides describe Walsh as a useful gauge for how far labor leaders are willing to bend before withdrawing their support and opposing legislation, which can significantly complicate its passage.

State Representative Garrett J. Bradley, who has helped raise money for Walsh’s mayoral campaign, said that when the Legislature was trying in 2011 to make changes to municipal employee health care plans, DeLeo dispatched him to meet with Walsh. “The speaker said, ‘Marty has some issues; go talk to him,’ ” said the Hingham Democrat.

Walsh and Bradley met with about a half-dozen representatives from police and firefighter unions and the state AFL-CIO in Walsh’s fifth-floor State House office. Ultimately, said Bradley, Walsh helped coax some leeway from the unions.

“We got to where we were going to get because when Marty told them that this was as far as we were going to get, they believed him,” Bradley said.

Finneran, who has donated to both Walsh’s and Connolly’s campaigns but is supporting Walsh, called him a stalwart during the ongoing battle over charter schools. As a traditional labor ally, Walsh came under heavy pressure from unions to resist the push for more charter schools, which employ nonunion teachers. Walsh, he said, was “willing to stand in the teeth of that opposition.”

“The teachers’ unions were doing everything they could to undermine charter schools, and without Marty standing up year after year after year, I’m not sure I would have won that fight.”

It is a niche Walsh has filled frequently, presenting unions’ concerns to legislative leaders and bouncing between the groups in search of a middle ground. Colleagues cite the 2006 health care expansion, the 2008 agreement to allow civilian flagmen at construction sites, and rules in the 2011 casino law surrounding construction of the gambling facilities.

Walsh’s friction with leadership has frequently revolved around spending. Finneran recalls Walsh arriving in the House in 1997 as a “bull in a china shop” who needed to be reminded that “we can’t be doing blank checks.”

“If there was tension in the relationship, it would have been because he was a passionate advocate and I was the guy who had to pour some cold water,” Finneran said.

Walsh acknowledged, “I felt that, in some areas, we should be putting more money into social programs because of the homelessness or the drug addiction or the mental health.”

In 2006, Walsh worked with a bipartisan group of lawmakers and Lieutenant Governor Kerry Healey, a Republican, to push through state funding for “sobriety high schools,” designed for students who have dealt with substance abuse.

But he has broken with leadership on social issues
, often taking a more liberal position.

One outlying area is Walsh’s strong rating among gun-rights groups. An 80 percent score from the Massachusetts-based Gun Owners Action League last year tied for the highest in the Boston delegation. Walsh’s camp notes that he has voted for an assault weapons ban and sponsored legislation to ban “cop killer” bullets. The votes on which GOAL scored lawmakers came not on fundamental gun-control issues, but on more technical questions, a spokesman said.

In one of his first major votes in the House, on whether to reinstate capital punishment, Walsh sided with Finneran in opposing the death penalty. In 2000, he lined up with the speaker again in opposing a bill creating a buffer zone preventing abortion protesters from coming within a proscribed perimeter of clinics.

Later, Walsh, who like Connolly has described himself as “personally prolife,” would vote to expand the buffer zone, and abortion-rights group rate his voting record as prochoice.

Walsh has bolstered his credibility among progressives for his work in protecting gay marriage rights, difficult votes at the time for a lawmaker in a heavily Irish Catholic district. Walsh’s voting record shows down-the-line support for gay marriage, including a key vote against a 2004 Finneran proposal to ban gay marriage while giving the Legislature the right to establish civil unions in the future. The constitutional amendment fell, 100 to 98.

“If Marty and one other move, my thing wins and you’ve got something significantly different in Massachusetts,” Finneran said.

The friction, which did not prevent Finneran from tapping Walsh to lead the newly created Homeland Security Committee in 2003, was perhaps the mildest of the occasionally sour relations Walsh has had with three House speakers.

In 2004, when Walsh backed Rogers against Salvatore F. DiMasi in the fight to succeed Finneran, it cost Walsh his chairmanship. After DiMasi won, he broomed out many of Finneran’s deputies in what some members termed “the Irish massacre.”

But, again, Walsh labored his way back into something like DiMasi’s good graces by offering his services in dealing with unions, say onetime aides to the former speaker.

At the dawn of DeLeo’s term, after again backing Rogers, Walsh was left again on the losing team. DeLeo declined to be interviewed for this article, but lawmakers close to him say that, while at times wary of Walsh, DeLeo has come to trust his skills at brokering agreements and, in particular, his ability to make peace with labor groups. He tapped Walsh to lead the oft-dormant House Ethics Committee.

“I think the speakers respected me in the sense that they know I’m hard-working and they know I can get to compromise,” Walsh said.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.

Thanks & Welcome to Globe.com

You now have unlimited access for the next two weeks.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.