Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

install socket io client

npm install socket.io-client
Comment

install socket.io

npm i socket.io
Comment

socket.io npm

// npm i socket.io
const http = require('http')
const express = require('express')
const socketio = require('socket.io')

const app = express()
const server = http.createServer(app)
const io = socketio(server)

const port = process.env.PORT || 3000

server.listen(port, () => {
  console.log(`Server is up on port ${port}!`)
})
Comment

socket.io node

/*The following example attaches socket.io to a plain Node.JS HTTP server listening on port 3000.*/

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);



//Standalone

const io = require('socket.io')();
io.on('connection', client => { ... });
io.listen(3000);
//Module syntax
import { Server } from "socket.io";
const io = new Server(server);
io.listen(3000);
                               
                               
                               
//In conjunction with Express
//Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function. Also make sure to call .listen on the server, not the app.

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
                               
                               
//In conjunction with Koa
//Like Express.JS, Koa works by exposing an application as a request handler function, but only by calling the callback method.

const app = require('koa')();
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
                               
                               
                               
//In conjunction with Fastify
//To integrate Socket.io in your Fastify application you just need to register fastify-socket.io plugin. It will create a decorator called io.

const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.io.on('connection', () => { /* … */ });
app.listen(3000);
     
Comment

socket io nodejs

import { Server } from "socket.io";

const io = new Server(3000);

io.on("connection", (socket) => {
  // send a message to the client
  socket.emit("hello from server", 1, "2", { 3: Buffer.from([4]) });

  // receive a message from the client
  socket.on("hello from client", (...args) => {
    // ...
  });
});
Comment

socket io nodejs

const express = require('express');const app = express();const http = require('http');const server = http.createServer(app);const { Server } = require("socket.io");const io = new Server(server);app.get('/', (req, res) => {  res.sendFile(__dirname + '/index.html');});io.on('connection', (socket) => {  console.log('a user connected');});server.listen(3000, () => {  console.log('listening on *:3000');});
Comment

Implement socket.io in node.js application controller

//app.js or index.js
const app = express();
var http = require("http");
var server=http.createServer(app).listen(2525, (req, res) => {
  console.log("Server running on", 2525);
});
var socketIO = require("socket.io");
var io = socketIO(server);
global.io = io //Importent line

//Add the below statement to your controller code
global.io.emit("eventname", "yourdata"); //Importent line
Comment

PREVIOUS NEXT
Code Example
Shell :: redis download 
Shell :: how to install any package in node.js 
Shell :: apache2 connection refused ubuntu 
Shell :: replace word in file linux command 
Shell :: crosh 
Shell :: add images in readme github file 
Shell :: how to sort tags on git tag 
Shell :: check pip library path ubuntu 
Shell :: how to find and kill a running process on a port in mac 
Shell :: how to push to heroku outside the master branch 
Shell :: cent os GUI install 
Shell :: helm add stable hub 
Shell :: push to github from terminal 
Shell :: squash 3 commit 
Shell :: how to remove git from ubuntu 21 
Shell :: checking git settings 
Shell :: find name file in cmd 
Shell :: convert vmdk to qcow2 
Shell :: how to update your local repository 
Shell :: mono-complete arch 
Shell :: Docker - Container is not running 
Shell :: edit text file bash 
Shell :: install aptitude ubuntu 20.04 
Shell :: git force add 
Shell :: batch file if statement 
Shell :: docker-compose: line 1: Not: command not found apt get 
Shell :: merge branch to branch 
Shell :: delete command from history 
Shell :: how to find ssh config file mac 
Shell :: docker image prune 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =