loading...

Much needed love, I just updated RxJS-Socket.io to 0.3.7

moshmage profile image moshmage ・2 min read

After a long year of developing for work, I could finally take on some break in between job and .. well, lets be serious: there isn't much else to do and gaming has become a chore - I was able to pour some time in and bring back my small project from the dead.

RE-introducing RxJs-Socket.io; A simple wrapper around socket.io and RxJs to make our lives way easier - the thought behind it is that I hated keeping track of all the events and callbacks and special cases and sharing them and.. now I don't have to! :D

Think of it like having a global stream for that specific event where you can simply import it, pipe it, and then subscribe and unsubscribe when you don't need it. And if you really want to, you can make it dead.

// events.ts
import {IO, ioEvent} from 'rxjs-socket.io';

export const socket = new IO();

const helloWorld = new ioEvent<{text: string}>('hello-world');
const showoff = new ioEvent<any>('name', !!uniqueEvent, +count, initialState);


export const IOEvents = {helloWorld, ping};


// some-other-file.ts
import {IOEvents, socket} from 'events.ts';

const helloStream$ = IOEvents.helloWorld.event$;

helloStream$.pipe(
                filter(event => event.text === 'hello world'),
                take(1))
            .subscribe(({text}) => {
                console.log('text should be "hello world"', text);
                // make it dead -- we only take 1, but this event is still hooked, if you make it dead you make it silent for every other subscription
                IOEvents.helloWorld.unhook()
            });


socket.listenToEvent(IOEvent.helloWorld);

const [you$, can$, also$] = socket.listen(['do', 'this', 'if-you-want']);

you$.subscribe(data => console.log('you$ stream', data))

socket.connect('protocol://address:port');

// server.ts

//...
socket.emit('hello-world', {text: 'hello world'});

Posted on by:

Discussion

pic
Editor guide