https://github.com/code4mk/now-socketio
💬 A short Journey of Socket.io
https://github.com/code4mk/now-socketio
socket-io socketio
Last synced: 7 months ago
JSON representation
💬 A short Journey of Socket.io
- Host: GitHub
- URL: https://github.com/code4mk/now-socketio
- Owner: code4mk
- Created: 2018-09-16T19:59:16.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-17T20:56:17.000Z (over 7 years ago)
- Last Synced: 2025-02-19T12:55:25.754Z (10 months ago)
- Topics: socket-io, socketio
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# now-socketio
A short Journey of Socket.io
### socketio Relationship

### server-side
```js
var serverIO = require('socket.io')(9000);
serverIO.on('connection', (socket) => {
// fetch
socket.on('message', (response) => {
console.log(response.name)
});
// sent
socket.emit('post', {
title: 'data server to cilent',
like: 100
})
socket.on('disconnect', () => { });
});
```
### client-side
```js
var socket = io('http://localhost:9000');
socket.on('connect', () => {
socket.send('hi');
// sent
socket.emit('message',{
id: 1,
name: 'socket.io'
})
// fetch
socket.on('post', (response) => {
console.log(response.title)
});
});
```
### emit cheetsheat

### emit
```js
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid (server-side)
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
// join to subscribe the socket to a given channel (server-side):
socket.join('some room');
// then simply use to or in (they are the same) when broadcasting or emitting (server-side)
io.to('some room').emit('some event'):
// leave to unsubscribe the socket to a given channel (server-side)
socket.leave('some room');
```
### sources
* [devdocs](http://devdocs.io/socketio/)
* [room socket](https://gist.github.com/mostafa6765/482d28caa02f59f6da12d13ea907e856)
* [room socket](https://gist.github.com/crtr0/2896891)
* [mongo socket 1](https://www.youtube.com/watch?v=8Y6mWhcdSUM)
* [mongo socket 2 / last](https://www.youtube.com/watch?v=hrRue5Rt6Is)
* [chat.io](https://github.com/OmarElGabry/chat.io/blob/master/app/socket/index.js)
* [angular](https://github.com/ShankyTiwari/Realtime-Private-Chat-using-AngularJs-Nodejs-and-Mysql)
* [socketio redis](https://github.com/socketio/socket.io-redis)
* [short journey socket medium](https://medium.com/code4mk-org/a-short-journey-of-socket-io-with-code4mk-86c9e198720e)