Skip to content

Instantly share code, notes, and snippets.

@EnricoPicci
Last active June 13, 2018 08:52
Show Gist options
  • Save EnricoPicci/35f3c3a2a2a3f96cfdf7b89d46a5d499 to your computer and use it in GitHub Desktop.
Save EnricoPicci/35f3c3a2a2a3f96cfdf7b89d46a5d499 to your computer and use it in GitHub Desktop.
Sockets as Observable
import { Server } from 'http';
import { Observable } from 'rxjs';
import { Observer } from 'rxjs';
import * as socketIoServer from 'socket.io';
import {SocketObs} from './socket-obs';
export function sockets(httpServer: Server, port) {
httpServer.listen(port, () => {
console.log('Running server on port %s', port);
});
return new Observable<SocketObs>(
(subscriber: Observer<SocketObs>) => {
socketIoServer(httpServer).on('connect',
socket => {
console.log('client connected');
subscriber.next(new SocketObs(socket));
}
);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment