0.0.2 • Published 8 months ago

@isimisi/sse.ts v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

sse.ts

this project was inspired by sse.js

installation

yarn add @isimisi/sse.ts

usage in react

export default function MyComponent() {
    const [source, setSource] = useState<Source | null>(null)
    const [message, setMessage] = useState<string>("")

    useEffect(() => {
        const src = Source.get('http://localhost:3333/sse')

        setSource(src);

        return () => {
            src.close()
        }
    }, [])

    useEffect(() => {
        if (source) {
            source.on('message', e => {
                setMessage(e.data.message);
            })
        }

        return () => {
            if (source) {
                source.off('message')
            }
        }
    }, [source])

    return <div>{message}</div>
};

API reference

TypeMethodreturnsargs
staticSource.getSourceurl: string | URLconfig
staticSource.postSourceurl: string | URLbody: objectconfig
publicsource.onvoidevent: stringlistener: (e: SourceEvent) => void
publicsource.offvoidevent: string
publicsource.closevoid
TypeInterface
Config{ headers?: Record<string, string | number | boolean>, withCredentials?: boolean }
SourceEventextends CustomEvent { data: any, id?: string, source?: Source, readyState?: number}
0.0.2

8 months ago

0.0.1

8 months ago