Git Product home page Git Product logo

Comments (4)

chrisleekr avatar chrisleekr commented on May 23, 2024 1

Hi @ihormartyniuk,

Good to hear and thank you for sharing the solution.

Hope you have good day.

Chris

from yii2-angular-boilerplate.

chrisleekr avatar chrisleekr commented on May 23, 2024

Hey @ihormartyniuk

For fixing 415 (Unsupported Media Type) is simply setting content negotiator in behaviors:

public function behaviors()
{
	//...existing code...
	$behaviors['contentNegotiator'] = [
		'class' => \yii\filters\ContentNegotiator::className(),
		'only' => ['index'],
		'formatParam' => '_format',
		'formats' => [
			'text/event-stream' => \yii\web\Response::FORMAT_RAW,
		],
	];
	//...existing code...
}

However, I was trying to make working example for you. But I got no luck as well.

What I did:

When angular code calls EventSource, the request hangs more than 45000 milliseconds and no results back.

If you succeed, could you share your code how you make it working?

from yii2-angular-boilerplate.

ihormartyniuk avatar ihormartyniuk commented on May 23, 2024

Hi, thanks for your full and informative answer for me. I got a step of app when have an error
Fetch API cannot load http://domain.my/sse/message. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. in my browser console. But so interesting that, status code is 200 OK and authorization is success)

I installed extension of EventSource wich use AlexGalays and add headers for authorization.
I added yii2-sse extension developed by odannyc.

Every time angular app trying to connect to my yii app and has previous error without any time delay.

I need your help with CORS.
And it will be done!)

from yii2-angular-boilerplate.

ihormartyniuk avatar ihormartyniuk commented on May 23, 2024

Hi @chrisleekr
I got success with my issue.

What I did:

  • Used https://github.com/odannyc/yii2-sse in Yii2

  • I installed extension of EventSource wich use AlexGalays in https://github.com/AlexGalays/EventSource and add headers for authorization.
    eventSource = new EventSource('http://domain.my/sse/message', { headers: {'Authorization': 'Bearer ' + token}});

  • Add your code to behavior in my Controller
    public function behaviors()
    {
    //...existing code...
    $behaviors['contentNegotiator'] = [
    'class' => \yii\filters\ContentNegotiator::className(),
    'only' => ['index'],
    'formatParam' => '_format',
    'formats' => [
    'text/event-stream' => \yii\web\Response::FORMAT_RAW,
    ],
    ];
    //...existing code...
    }

  • Action in my Controller
    $sse = Yii::$app->sse;
    $sse->set('allow_cors', true);
    $sse->set('keep_alive_time', 900);
    $sse->addEventListener('message', new MessageEventHandler());
    $sse->start();
    $sse->flush();

Thanks.

from yii2-angular-boilerplate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.