Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to use socket io with express in the same time

import express from 'express'
import http from 'http'
import { Server } from 'socket.io'

const expressApp = express()
const server = http.createServer(expressApp)

const io = new Server(server, {
  cors: {
    origin: allowedOrigins, // string[]
    methods: ['GET', 'POST'],
    allowedHeaders: [],
    credentials: true
  }
})

expressApp.get('/test', (req, res, next) => {
	res.sendStatus(200)
})

server.listen(80)

io.on('connection', () => {})
Comment

socket.io with express

// 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 express

const app = require("express")();const httpServer = require("http").createServer(app);const options = { /* ... */ };const io = require("socket.io")(httpServer, options);io.on("connection", socket => { /* ... */ });httpServer.listen(3000);// WARNING !!! app.listen(3000); will not work here, as it creates a new HTTP server
Comment

PREVIOUS NEXT
Code Example
Javascript :: vite install in vue 
Javascript :: datatable column width 
Javascript :: object element by index javascript 
Javascript :: back button next js 
Javascript :: jquery event element is visible 
Javascript :: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build. 
Javascript :: toggle classname onclick react 
Javascript :: iterate through list js 
Javascript :: javascript execute string code 
Javascript :: js get bytearray from file 
Javascript :: formik clear field 
Javascript :: javascript multiline string 
Javascript :: click outside box jquery 
Javascript :: infinite loop in programming 
Javascript :: componentdidupdate 
Javascript :: typescript css variables 
Javascript :: redux useselector 
Javascript :: reverse a linked list javascript 
Javascript :: ReferenceError 
Javascript :: datatable cdn 
Javascript :: jquery checkbox 
Javascript :: material ui jss media query 
Javascript :: use thymeleaf variable in javascript 
Javascript :: how to reverse loop in javascript 
Javascript :: jquery confirm dialog 
Javascript :: how to detect onend reach of scrollview in react native 
Javascript :: replace string in javascript 
Javascript :: javascript replace doublequote with empty string 
Javascript :: bash commands in node 
Javascript :: redux append to an array 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =