DEV Community

moshmage
moshmage

Posted on

1

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

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'});

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!