Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RxJS 6.x WebSocketSubject client

Tags:

rxjs

websocket

I cannot figure out how to use WebSocketSubjects in rxjs v6.x

Here's the working HTML/JS for v5.5.6. The commented out code is my attempt at getting it working in v6.x:

<html>
<head>
    <!-- <script src="https://unpkg.com/@reactivex/[email protected]/dist/global/rxjs.umd.js"></script> -->
    <script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script>
    <script>
        // const { WebSocketSubject } = rxjs.webSocket;
        // const socket$ = WebSocketSubject.create('ws://localhost:8080');
        const socket$ = Rx.Observable.webSocket('ws://localhost:8080');
        socket$.subscribe(
                (data) => console.log(data),
                (err) => console.error(err),
                () => console.warn('Completed!')
            );
        socket$.next(JSON.stringify({
            event: 'events',
            data: 'test',
        }));
        console.log('here')
    </script>
</head>
<body></body>
</html>
like image 992
Corey Cole Avatar asked Nov 29 '25 11:11

Corey Cole


1 Answers

I got it working with [email protected]. As I suspected, I was just using the version 6 syntax wrong. See working example:

<html>

<head>
    <script src="https://unpkg.com/@reactivex/[email protected]/dist/global/rxjs.umd.js"></script>
    <script>
        const { WebSocketSubject } = rxjs.webSocket;
        const socket$ = new WebSocketSubject('ws://localhost:8080');
        socket$.subscribe(
            (data) => console.log(data),
            (err) => console.error(err),
            () => console.warn('Completed!')
        );
        socket$.next({
            event: 'events',
            data: 'test',
        });
        console.log('here')
    </script>
</head>

<body></body>

</html>
like image 88
Corey Cole Avatar answered Dec 02 '25 03:12

Corey Cole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!