How to use ionic with ratchet php library

I am trying to use php ratchet library (not node server script) as my socket server, however when I try to connect to it, I get cross origin request issue. Here is my ratchet server code:

require 'vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use ChatApp\Chat;

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()


Here is my code to connect to it via ionic app:

import { Component, NgZone, ViewChild } from '@angular/core';
import { NavController, Content } from 'ionic-angular';

import * as io from '';

  selector: 'page-home',
  templateUrl: 'home.html'

export class HomePage {

  @ViewChild(Content) content: Content;

  messages: any = [];
  socketHost: string = "http://localhost:3000";
  socket: any;
  chat: any;
  username: string;
  zone: any;

  constructor(public navCtrl: NavController) {
    this.socket = io.connect(this.socketHost); = new NgZone({ enableLongStackTrace: false });
    this.socket.on("chat message", (msg) => { => {

  chatSend(v) {
    let data = {
      message: v.chatText,
      username: this.username

    this.socket.emit('new message', data); = '';


Can anyone please help?


okay i figured this out.

Hi, could you please share your code as I am also encountering similar situation!