currently i am trying to send and get the data via angular client to socket server and from socket server to angular i need to get data i able to push the data but i need to know how can i push data to the api which is there in socket server and get data from the api to socket server and emit it to client
below is my
For sending data from angular client to socket server
component code
constructor(public socketService: SocketioService){
} ngOnInit(){ this.socketService.setupSocketConnection(); } // For sending post request sendMsg(){ this.socketService.sendData(this.title); } // For getting the request getMsg(){ this.socketService.getMsg().subscribe(res => { console.log(res); })
Angular service code
import * as io from 'socket.io-client';
import { Observable } from 'rxjs';
socket;
constructor() {
}
setupSocketConnection() {
this.socket = io(environment.SOCKET_ENDPOINT);
}
// for posting data
sendData(values){
console.log(values);
this.socket.emit('my message', values);
}
//for getting data
getMsg(){
return Observable.create((observer) => {
this.socket.on('grabMsg', (message) => {
observer.next(message);
});
});
}
Node server code
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.get('/', (req, res) => {
res.send('<h1>Hey Socket.io</h1>');
});
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('my message', (msg) => {
//here i want to consume api like
// localhost:3000(post) {"title":"ss"}
console.log('message: ' + msg);
});
socket.on('grabMsg', () => {
//here i want to consume api like
// localhost:3000(get)
let ms = 'max'
io.emit(ms);
});
});
http.listen(3001, () => {
console.log('listening on *:3001');
});
so here how can i send and post data in socket server in short i will send data to from angular client to socket server then to some api