Skip to content

Commit

Permalink
Merge pull request #7 from aviabird/phoenix
Browse files Browse the repository at this point in the history
Working on Phoenix Socket Channel implementation on comments
  • Loading branch information
pkrawat1 authored Dec 20, 2016
2 parents 778bd7a + d2a0c83 commit 1042a9e
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"ng2-progressbar": "^1.0.3",
"ng2-slim-loading-bar": "^2.0.5",
"ng2-tag-input": "^0.4.4",
"phoenix_js": "0.0.3",
"reselect": "^2.5.4",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
Expand Down
24 changes: 12 additions & 12 deletions src/app/actions/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const ActionTypes = {
LOAD_COMMENTS_SUCCESS: type('[Comment] Load Comments Success'),
ADD_COMMENT: type('[Comment] Add Comment'),
ADD_COMMENT_SUCCESS: type('[Comment] Add Comment Success'),
SAVE_COMMENT: type('[Comment] Save Comment'),
SAVE_COMMENT_SUCCESS: type('[Comment] Save Comment Success'),
// SAVE_COMMENT: type('[Comment] Save Comment'),
// SAVE_COMMENT_SUCCESS: type('[Comment] Save Comment Success'),
DELETE_COMMENT: type('[Comment] Delete Comment'),
DELETE_COMMENT_SUCCESS: type('[Comment] Delete Comment Success'),
};
Expand Down Expand Up @@ -53,17 +53,17 @@ export class AddCommentSuccessAction implements Action {
constructor(public payload: Comment) { };
}

export class SaveCommentAction implements Action {
type = ActionTypes.SAVE_COMMENT;
// export class SaveCommentAction implements Action {
// type = ActionTypes.SAVE_COMMENT;

constructor(public payload: Comment) { };
}
// constructor(public payload: Comment) { };
// }

export class SaveCommentSuccessAction implements Action {
type = ActionTypes.SAVE_COMMENT_SUCCESS;
// export class SaveCommentSuccessAction implements Action {
// type = ActionTypes.SAVE_COMMENT_SUCCESS;

constructor() { };
}
// constructor() { };
// }

export class DeleteCommentAction implements Action {
type = ActionTypes.DELETE_COMMENT
Expand All @@ -86,7 +86,7 @@ export type Actions
| LoadCommentsSuccessAction
| AddCommentAction
| AddCommentSuccessAction
| SaveCommentAction
| SaveCommentSuccessAction
// | SaveCommentAction
// | SaveCommentSuccessAction
| DeleteCommentAction
| DeleteCommentSuccessAction
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { NotificationEffects } from './effects/notification';
import { ToasterService, ToasterModule } from 'angular2-toaster/angular2-toaster';
import { CanActivateViaAuthGuard } from './guards/authenticated';
import { CanEditPinGuard } from './guards/can-edit-pin';
import { PhoenixChannelService } from './services/phoenix-channel.service';

// Must export the config
export const firebaseConfig = {
Expand Down Expand Up @@ -98,7 +99,8 @@ export const firebaseConfig = {
},
ToasterService,
CanActivateViaAuthGuard,
CanEditPinGuard
CanEditPinGuard,
PhoenixChannelService
],
bootstrap: [AppComponent]
})
Expand Down
25 changes: 18 additions & 7 deletions src/app/components/shared/footer/footer.component.html
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>
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

@media only screen and (min-width: 40.063em) {
.footer .columns:nth-child(n+2) {
border-left: 1px solid scale-color(#555);
border-left: 1px solid rgba(85, 85, 85, 0.05);;
min-height: 280px;
}
}
Expand All @@ -50,3 +50,7 @@
.dropdown.menu > li.is-dropdown-submenu-parent > a::after{
border-color: #805840 transparent transparent;
}

.fa-heart{
color: red;
}
2 changes: 1 addition & 1 deletion src/app/components/shared/footer/footer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, OnInit } from '@angular/core';
@Component({
selector: 'pin-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.css']
styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {

Expand Down
84 changes: 84 additions & 0 deletions src/app/services/phoenix-channel.service.ts
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));
}
})
}

}
4 changes: 3 additions & 1 deletion src/app/services/pin-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ApiUrl } from '../app.api-url';
import { HttpService } from './http';
import { AppState, getPinscountWithSeatchQuery } from '../reducers/index';
import { Store } from '@ngrx/store';
import { PhoenixChannelService } from './phoenix-channel.service';

@Injectable()
export class PinDataService {
Expand All @@ -20,7 +21,8 @@ export class PinDataService {
constructor(
public db: AngularFireDatabase,
public http: HttpService,
public store: Store<AppState>
public store: Store<AppState>,
private phoenixChannelService: PhoenixChannelService
) {
this.store.select(getPinscountWithSeatchQuery()).subscribe(
params => {
Expand Down
1 change: 1 addition & 0 deletions src/environments/environment.prod.ts
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')
};
1 change: 1 addition & 0 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export const environment = {
production: false,
// apiEndpoint: 'https://pinwork-api.herokuapp.com/api/',
apiEndpoint: 'http://localhost:4000/api/',
socketEndpoint: 'ws://localhost:4000',
basic_auth_token: btoa('api:password')
};
11 changes: 11 additions & 0 deletions src/typings.d.ts
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;
}
}

0 comments on commit 1042a9e

Please sign in to comment.