-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from aviabird/phoenix
Working on Phoenix Socket Channel implementation on comments
- Loading branch information
Showing
11 changed files
with
140 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
<div class="footer"> | ||
<div class="row full-width"> | ||
<div class="small-12 medium-3 large-4 columns"> | ||
<div class="row"> | ||
<div class="small-12 medium-4 large-5 columns"> | ||
<i class="fa fa-laptop fa-4"></i> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cum maiores alias ea sunt facilis impedit fuga dignissimos illo quaerat iure in nobis id quos, eaque nostrum! Unde, voluptates suscipit repudiandae!</p> | ||
<p>Built with love <span class="fa fa-heart"></span> <a href="https://medium.com/aviabird">@aviabird</a></p> | ||
<p> | ||
We’re excited to start this new adventure, and work more closely with enterprise teams working on Angular 2/Ruby on rails/Phoenix Framework/GoLang applications. | ||
Think we can help your team? Reach out at [email protected] | ||
</p> | ||
</div> | ||
<div class="small-12 medium-3 large-4 columns"> | ||
|
||
<div class="small-12 medium-5 large-5 columns"> | ||
<i class="fa fa-code fa-4"></i> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit impedit consequuntur at! Amet sed itaque nostrum, distinctio eveniet odio, id ipsam fuga quam minima cumque nobis veniam voluptates deserunt!</p> | ||
<h4>WHO AM I?</h4> | ||
<p> | ||
I'm a Rubyist from Pune. Founder of <a href="https://medium.com/aviabird/introducing-aviabird-technologies-929db42e342d#.z1h59tnkw">Aviabird Techonolgy</a>. | ||
</p> | ||
</div> | ||
<div class="small-6 medium-3 large-2 columns"> | ||
|
||
<!--<div class="small-6 medium-3 large-2 columns"> | ||
<h4>Work With Me</h4> | ||
<ul class="footer-links"> | ||
<li><a href="#">What I Do</a></li> | ||
|
@@ -17,13 +26,15 @@ <h4>Work With Me</h4> | |
<li><a href="#">Blog</a></li> | ||
<li><a href="#">FAQ's</a></li> | ||
</ul> | ||
</div> | ||
</div>--> | ||
<div class="small-6 medium-3 large-2 columns"> | ||
<h4>Follow Me</h4> | ||
<ul class="footer-links"> | ||
<li><a href="https://github.com/pkrawat1">GitHub</a></li> | ||
<li><a href="https://www.facebook.com/pkrawat1">Facebook</a></li> | ||
<li><a href="https://twitter.com/pkrawat1">Twitter</a></li> | ||
<li><a href="https://www.facebook.com/aviabird">AviaBird Technology</a></li> | ||
<li><a href="https://www.facebook.com/AngularCasts/">AngularCasts</a></li> | ||
</ul> | ||
</div> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Socket } from "phoenix_js" | ||
import { environment } from '../../environments/environment'; | ||
import { Store } from '@ngrx/store'; | ||
import { AppState, getUserAuthStatus, getAuthUser } from '../reducers/index'; | ||
import { storeFreeze } from 'ngrx-store-freeze'; | ||
import { LoadCommentsSuccessAction, DeleteCommentSuccessAction } from '../actions/comment'; | ||
import { User } from '../models/user'; | ||
|
||
@Injectable() | ||
export class PhoenixChannelService { | ||
private socket: any; | ||
private user: User; | ||
|
||
constructor( | ||
private store: Store<AppState> | ||
) { | ||
this.store.select(getUserAuthStatus).subscribe( | ||
authStatus => { | ||
if (!authStatus) { | ||
if(this.socket) { | ||
this.socket.disconnect(); | ||
} | ||
} | ||
else { | ||
// Incase Socket is not present | ||
if(!this.socket) { | ||
this.socket = this._socket(); | ||
this.store.select(getAuthUser).subscribe( | ||
user => this.user = user | ||
); | ||
} | ||
|
||
this.socket.connect(); | ||
|
||
this.socket.onOpen(ev => console.log("OPEN", ev)); | ||
this.socket.onError(ev => console.log("ERROR", ev)); | ||
this.socket.onClose(e => console.log("CLOSE", e)); | ||
|
||
this.initCommentChannel(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
private _socket(){ | ||
return new Socket( | ||
`${environment.socketEndpoint}/socket`, | ||
{ | ||
params: { | ||
token: localStorage.getItem('access_token') | ||
}, | ||
transport: WebSocket, | ||
logger: ((kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }) | ||
}, | ||
); | ||
} | ||
|
||
private initCommentChannel() { | ||
var chan = this.socket.channel("comments:lobby", {}); | ||
chan.join().receive("ignore", () => console.log("auth error")) | ||
.receive("ok", () => console.log("join ok")); | ||
chan.onError(e => console.log("something went wrong", e)); | ||
chan.onClose(e => console.log("channel closed", e)); | ||
|
||
// chan.on("user:entered", msg => { | ||
// var username = msg.user || "anonymous"; | ||
// console.log("User Entered: ", username); | ||
// }) | ||
|
||
chan.on("new:msg", msg => { | ||
if (this.user.id != msg.user_id) { | ||
this.store.dispatch(new LoadCommentsSuccessAction([msg])); | ||
} | ||
}) | ||
|
||
chan.on("delete:msg", msg => { | ||
if (this.user.id != msg.user_id) { | ||
this.store.dispatch(new DeleteCommentSuccessAction(msg.id)); | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export const environment = { | ||
production: true, | ||
apiEndpoint: 'https://pinwork-api.herokuapp.com/api/', | ||
socketEndpoint: 'ws://pinwork-api.herokuapp.com', | ||
basic_auth_token: btoa('api:password') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
// Typings reference file, you can add your own global typings here | ||
// https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html | ||
declare module 'phoenix_js' { | ||
class Socket { | ||
constructor(endPoint: any, opts: any) | ||
connect(): void; | ||
disconnect(callback: any, code: any, reason: any): void; | ||
onOpen(callback: any): void; | ||
onError(callback: any): void; | ||
onClose(callback: any): void; | ||
channel(topic: any, chanParams?: {}): any; | ||
} | ||
} |