Skip to content

Instantly share code, notes, and snippets.

@Ibro
Created May 5, 2017 18:10
Show Gist options
  • Save Ibro/a535c10a348370650d184079a957f46c to your computer and use it in GitHub Desktop.
Save Ibro/a535c10a348370650d184079a957f46c to your computer and use it in GitHub Desktop.
RxJS Socket.IO Angular Chat - Angular Service - Coding Blast - www.codingblast.com
import * as io from 'socket.io-client';
import { Observable } from 'rxjs/Observable';
export class ChatService {
private url = 'http://localhost:3000';
private socket;
constructor() {
this.socket = io(this.url);
}
public sendMessage(message) {
this.socket.emit('new-message', message);
}
public getMessages = () => {
return Observable.create((observer) => {
this.socket.on('new-message', (message) => {
observer.next(message);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment