Ridesharing has been emerging as a new type of mobility. However, the early promises of ridesharing for alleviating congestion in cities may be undermined by a number of challenges, including the growing number of proposed services and the subsequent increasing number of vehicles, as a natural consequence of competition. In this work, we present optimization-based approaches to model cooperation and competition between multiple ridesharing companies, in a real-time on-demand setting. A recent trend relies on solving the integrated combination of Dial-A-Ride Problems (DARP), which compute the cost of assigning incoming requests to vehicle routes, plus Linear Assignment Problems (LAP), which assign vehicles to requests. While the DARPs, are solved at the level of the vehicles of each company, we introduce cooperative and competitive approaches to solve the LAP. The cooperative model, which could make use of Mobility as a Service platforms, is shown to solve the LAP to optimality following closely results from the literature, and limiting the amount of information the companies are required to share. We investigate how a realistic model of competition deviates from this optimality and provide worst case bounds. We evaluate these models with respect to a centralized model on one-week instances of the New York City taxi dataset. Model variants coping with noise in the travel time estimations, bias in the assignment costs, and preferences in the competitive case are also presented and validated. The computational results suggest that cooperation among ridesharing companies can be conducted in such a way to limit the degradation of the level of service with respect to a centralized model. Finally, we argue that the competition can lower the quality of the ridesharing service, especially in the case customer preferences are accommodated.