raviagheda / get-started-socket.io-mean

Getting started with Socket.io using MEAN Stack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

socket.io-mean

Guide: How to work with Socket.io + Mean Stack

socket.io

in express app

npm install socket.io

in /bin/www

var io = require('socket.io')(server);
require('../socket/base')(io);

in angular

npm install socket.io-client

in app.component.ts

import { Component, OnInit } from '@angular/core';
import { SocketService } from './socket.service';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'angular';
  constructor( private socketService: SocketService ){}

  ngOnInit(){
    this.socketService.connectSocket();
  }
}

Create angular service using ng g s socket

in app/socket.service.ts

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';

@Injectable({
  providedIn: 'root'
})
export class SocketService {
  socket;
  socket_endpoint = 'http://localhost:3000';

  constructor() { }
  
  connectSocket(){
    this.socket = io(this.socket_endpoint);
  }
}

About

Getting started with Socket.io using MEAN Stack

License:MIT License


Languages

Language:TypeScript 55.6%Language:JavaScript 32.3%Language:HTML 11.0%Language:CSS 1.1%