Skip to content

Instantly share code, notes, and snippets.

@parthghiya
Created February 7, 2017 13:05
Show Gist options
  • Save parthghiya/7a03cbf2bb5be5af7746e06a0d7d24fe to your computer and use it in GitHub Desktop.
Save parthghiya/7a03cbf2bb5be5af7746e06a0d7d24fe to your computer and use it in GitHub Desktop.
socket io service Angular 2
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import * as io from "socket.io-client";
import { IMessage, ISocketItem } from "../../models";
@Injectable()
export class SocketService {
private name: string;
private host: string = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port;
socket: SocketIOClient.Socket;
constructor() {}
// Get items observable
get(name: string): Observable<any> {
this.name = name;
let socketUrl = this.host + "/" + this.name;
this.socket = io.connect(socketUrl);
this.socket.on("connect", () => this.connect());
this.socket.on("disconnect", () => this.disconnect());
this.socket.on("error", (error: string) => {
console.log(`ERROR: "${error}" (${socketUrl})`);
});
// Return observable which follows "create" and "remove" signals from socket stream
return Observable.create((observer: any) => {
this.socket.on("create", (item: any) => observer.next({ action: "create", item: item }) );
this.socket.on("remove", (item: any) => observer.next({ action: "remove", item: item }) );
return () => this.socket.close();
});
}
// Create signal
create(name: string) {
this.socket.emit("create", name);
}
// Remove signal
remove(name: string) {
this.socket.emit("remove", name);
}
// Handle connection opening
private connect() {
console.log(`Connected to "${this.name}"`);
// Request initial list when connected
this.socket.emit("list");
}
// Handle connection closing
private disconnect() {
console.log(`Disconnected from "${this.name}"`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment